Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-22963

New value type that the server translates to the server date like $CurrentDate

    • Type: Icon: New Feature New Feature
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Write Ops
    • Query

      Having a value type that will be set to the server date when the field hits the Mongo server would be extremely useful for cases where you need to ether serialize complete objects to Mongo to replace an existing document or when you need to set the server date on date fields on sub objects in a document when you are replacing or updating the part or the whole document.

      In our case we have a number of objects that serialize to a document and all these objects/documents contain a sub document that contains audit info like the user, the application, user supplied updated date and the Mongo server systemdate.

      Due to this every field on our objects need to be serialized by hand and even then we can have difficulty because the Mongo current date is a command not a value type so we can't just set it on the fields we want it on in the document. Instead we need to make sure the field is not considered to be embedded in a sub document that is being updated or replaced because Mongo Current Data command will try to update the same sub document and the server will complain. If we could set a value instead that gets replaces with the current server date the issues goes away.

      We are running many servers around the world doing concurrent reads and updates so we need to use the current server date to know when an object has been updated vs just setting it on the Mongo client side.

      In the below example "$currentDate" represents where the new BSON type would go. Clearly our objects/documents are much more complicated than this.

      {
          "aa":"F1",
          "ab":"F2",
          "ac":"F3",
          "ad":"F4",
          "audit":
              {
                 "user":"xxx",
                 "dbDate":"$currentDate", 
                 "system":"yyyy"
              }
      }
      

      This would also have a bigger affect on people using object serialization. Currently the serialization of the below classes can not be done using the current Mongo driver and the $CurrentDate command. The sub document the date is on is being replaced so you need to write your own code to do this serialization.

      Later on the drivers could even implement a special type for this as well if they wanted or you could just override the serialize on the sub object to serialize that 1 required field different to use the new BSON value type.

          public class MediaData
          {
              public int Id { get; set; }
      
              public DateTime MediaDate { get; set; }
      
              public UpdateDetails UpdateDetails { get; set; }
      
              public int Status { get; set; }
      
              public Dictionary<string, MediaText> MediaTexts { get; set; }
      
              public string Publication { get; set; }
          }
      
          public class UpdateDetails
          {
              public string UpdateUser { get; set; }
      
              public string UpdateSource { get; set; }
      
              public DateTime? UpdateDate { get; set; }
      
              public DateTime? LastUpdatedDbTime { get; set; }
          }
      

      This would allow the current server date to be embedded at any level in the document, allow it to be set with the "Set" command in an update, be included in whole document operations like insert or replace as well as update.

      The Server date really is a value not a command and really should be treated as such in BSON. I know you may need to run a command server-side to get it but you are trying to get a value to be the value of a field so it really should be a value.

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            gilliganc@dnb.com.au Chris Gilligan
            Votes:
            7 Vote for this issue
            Watchers:
            20 Start watching this issue

              Created:
              Updated:
              Resolved: