hello. I am a developer trying to develop an application with MongoDB as the main repository. If you have any questions, please leave a question.
The project I am currently developing has application instance group A and application instance group B. Both operate as multi-instance, group A saves documents in DB in parallel, and group B reads and uses documents stored by A.
There is one limitation here. The point at which the document is stored in the DB is very important. When Group A’s instances save documents in parallel, they need to be able to recognize exactly when a document is saved to the DB when Group B retrieves a document.
Therefore, I would like a feature where MongoDB, not the application, automatically initializes the document’s creation-time fields when the document is saved.
To solve this, I initially tried to use ObjectId. From reading the official description, it’s because when you save the document, if you leave the _id field blank, it automatically initializes the field, and the ObjectId contains a timestamp within it. However, reading the official description, if the _id field is empty, it is initialized by the driver, not the DB, so this couldn’t be a solution.
I found $currentDate as another solution. If you use $currentDate when upsert the document, this also seems to satisfy your requirements.
I have a question for you here. Where does the logic to initialize the time run when a document is upsert using $currentDate? I’d like it to be initialized to the server’s native time, but I don’t think I can use this workaround if the document is initialized using the application’s system time.
Thanks to everyone who replies.
ps. sorry for my english skill.