[SERVER-22963] New value type that the server translates to the server date like $CurrentDate Created: 04/Mar/16 Updated: 06/Dec/22 Resolved: 24/Jul/19 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Write Ops |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Chris Gilligan | Assignee: | Backlog - Query Team (Inactive) |
| Resolution: | Duplicate | Votes: | 7 |
| Labels: | 3.2 | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||||||
| Assigned Teams: |
Query
|
||||||||||||||||||||
| Participants: | |||||||||||||||||||||
| Description |
|
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.
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.
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. |
| Comments |
| Comment by Craig Homa [ 24/Jul/19 ] |
|
Closing this as it is a dupe of SERVER-13695. |
| Comment by Asya Kamsky [ 30/Jun/19 ] |
|
I was going to leave this open because we don't have $$NOW equivalent for insert but that's tracked by SERVER-13695.
Will schedule this ticket for triage to see if there is work left that we plan to do.
|
| Comment by Asya Kamsky [ 29/Jun/19 ] |
|
You can see some examples here. We also added expressions (variables) in aggregation which represent current server datetime ($$NOW) and cluster time ($$CLUSTER_TIME) which can be used in update pipelines. |
| Comment by Andrew Ryder (Inactive) [ 09/Mar/16 ] |
|
This would also permit $setOnInsert to specify a field with server time value. For example, if you have a schema with "createdAt" and "lastModified" time, you can't use an upsert to instantiate the value of "createdAt". The only way I know of to implement this schema (correctly) is to always issue an insert first, followed by an update only if the insert fails. |