title: Setting a default DateTime.Now value to save to MongoDB when Document is saved
body: <p>I am very new to C#, having come from the ruby world, the ruby mongodb driver provided a <code>created_at</code> and <code>updated_at</code> field for all documents on saving and updating. Unfortunately after searching online it doesnt seem like this is a common thing in c#.</p>
  
  <p>I would like to know how would I default a documents <code>CreatedAt</code> field to <code>Time.Now</code> when persisting?</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using MongoDB.Bson;
  using MongoDB.Bson.Serialization.Attributes;
  using MongoDB.Driver;
  
  namespace MongoDBConsole
  {
      class Program
      {
          public static void Main(string[] args)
          {
              var client = new MongoClient("mongodb://localhost:27017");
              var database = client.GetDatabase("mongodb_console");
              var collection = database.GetCollection&lt;Person&gt;("person");
              var person = new Person
              {
                  Name = "John Doe",
                  Age = 25
              };
              collection.InsertOne(person);
  
          }
      }
  
      class Person
      {
          public string Name { get; set; }
          [BsonIgnoreIfNull]
           public int? Age { get; set; }
          [BsonDefaultValue(DateTime.Now)] // &lt;-- this errors out
          private DateTime CreatedAt { get; set; }
      }
  }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>My purpose for asking the question is to prevent myself from having to add a <code>CreatedAt</code> field every time I instantiate a new <code>Person</code> class object. <code>CreatedAt</code> should be automatically set whenever the <code>Person</code> class is persisted to MongoDB. One could easily set it every time a document is saved, but that's not very DRY. I am hoping to duplicate functionality that exists in another programming language. </p>