title: How to set auto-generated created_at/updated_at fields to mongodb collection with golang mongo-driver?
body: <p>Currently, I handle the <code>created_at</code> and <code>updated_at</code> fields myself, as part of the application logic.</p>
  <p>I'm now using <a href="https://www.mongodb.com/docs/upcoming/core/transactions/?_ga=2.83330399.876438863.1654091816-1779239596.1638477453" rel="nofollow noreferrer">multi-document transactions</a> for a bulk upsert: from the <code>n</code> documents specified, those which already exist in the collection are only updated, and those which doesn't, are inserted.</p>
  <p>The problem is when an already existing document is updated, it's <code>created_at</code> field is updated too with the current date, which is incorrect for my business logic. If I don't specify a <code>created_at</code> field in this operation, then updated documents are fine, but the newly inserted ones are left without their creation time.</p>
  <p>Having the driver/database itself deciding when to set both <code>created_at</code> and <code>updated_at</code> would be a solution since it is the driver/database itself who knows what kind of operation is being performed on each document.</p>
  <p>With <code>Node.js</code> you can do the following:</p>
  <pre><code>schema.set('timestamps', true);
  </code></pre>
  <p>And <code>createdAt</code> and <code>updatedAt</code> fields will be automatically added to your schema and managed by mongo.</p>
  <p>But I haven't been able to find something similar for the <a href="https://www.mongodb.com/docs/drivers/go/current/" rel="nofollow noreferrer">MongoDB Go Driver</a>.</p>
  <p>Has anyone done something similar?</p>