linq - MongoDB/C# Update Collection entries -


hello have mongodb collection called nodes structure follows:

{   "_id" : new bindata(3, "ljot2wcbskwg9vobsla0zq=="),    "logicalname" : "test node",    "host" : "eh5tmb054pc",    "port" : 104,    "appendtimestamp" : false,    "studies" : ["1.3.12.2.1107"],    "tests" : [],     "mainentries" : [{   "_id" : "1.3.12.2.1107",   "index" : 0,   "type" : "study" }] } 

i created new key called "mainentries" storing "studies" , "tests". in order support new versions without hassle, want write method in setup helper, enable me read collection - check if studies,tests exists , if yes add key "mainentries" , remove studies/tests key.

my question is: kind of query must use reach each collection of nodes check fields , update. using mongodb-csharp community driver.

would appreciate , pointers.

you can check whether field(s) still exist(s):

var collection = db.getcollection<node>("nodes"); var nodes = collection.find(query.and( // might want query.or instead?                 query<node>.exists(p => p.tests),                 query<node>.exists(p => p.studies)).setsnapshot();  foreach(var node in nodes) {   // maybe want move tests , studies mainentries here?   node.mainentries = new list<mainentries>();   node.test = null;   node.studies = null;   collection.update(node); } 

if don't want migrate data, remove fields , create new ones, can in simple batch update using $exists, $set , $remove


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -