MongoDB : update entire document except _id using C# driver

给你一囗甜甜゛ 提交于 2019-12-22 09:08:16

问题


I have to update all the fields except _id. I want to avoid to manually update the 16 fields... All the new fields are stored inside a BsonDocument

Thanks for ideas


回答1:


As @Philipp hinted there is a way way to do this. You can actually use the save function ( http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-Save%3CTDocument%3Emethod ) which will do what he says for you in the database end.

So imagine you have a document of:

{
    _id: {},
    d: 1
}

And that _id already exists, it will replace the previous document with this one.

Neat huh?




回答2:


When I understood you correctly, you have a document B with new data and you want it to completely replace document A.

In that case you can just set the _id field of document B explicitely to the value of the _id field of document A. When you then save document B, it will replace document A in the database.

When a saved document has the id of an existing document, the database will treat it like a new version of that document, not as a new document.



来源:https://stackoverflow.com/questions/14336116/mongodb-update-entire-document-except-id-using-c-sharp-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!