c# - Upserting in Mongo DB and the Id problem -
I am having trouble raising mongo db using the official C # driver.
Public abstract class AggregateRoot // & lt; Summary & gt; /// All mongoDb documents should have an ID, we specify it here /// & lt; / Summary & gt; Protected Collected Root () {ID = Object. GenerateNewId (); } [BSOID] Public Object ID (Receive); Set; }} My organization already has an ID, but I had to make Mongo special ID to work because all the documents in the collection should be one. Now I get a new unit in my system, a new Mongo ID is generated and I can not change the exception Manga a document old . Is there a work-around? Let me describe a little bit of design. All the institutions, which will be stored in the form of documents, were obtained from the collected route, in which the ID was generation. In each sub-document, my ID was generated automatically and I had no problem with it. Incidentally, the ID was started to fix the problem when the data was received from the mapocovalation and the generation was introduced so that ID-S is different. Now we can move ID generation to avoid ways because a new ID is in the new unit for updating. But this would mean that every god of the team should not forget to create IDS in the repository which is risky. If possible, it would be nice to ignore the ID compared to mapping from Mongo, and
It seems that you can clearly set the id value for both inserts and updates. It's okay to insert, all new objects require a _id value, however for updating you agree to change the value of _id on an existing document is not. / P> Try not to set all id values. If you do not specify a value before inserting, then the driver uses the built in IdGenerator sections to create a new _id value, so if it is an ObjectId type, then it will be ObjectIdGenerator . Then both your inserts and updates work fine.
Comments
Post a Comment