Domanda

Possible Duplicate:
How to update and upsert mulitple documents in MongoDB using C# Drivers

This may be a silly question. Actually I am confused with the syntax. An update from shell has this format:

db.collection.update(query,update,options)

where options are for upsert and multi flags. I can write something like this in the shell and it works:

db.users.update({"Gender":"female"},{$set:{"Hubby_name":1}},false,true)})

to mean that find all females ( since multi is true) and in their documents, add the key "Hubby name". If no female is found, don't do anything (since upsert is false).

Now how do I specify this (both the flags) in C# code? I am able to add only one flag in the Update method. The next parameter prompted by intellisence is SafeMode which I am not interested in. Also, what is the default behavior when I don't give any options at all?

È stato utile?

Soluzione

UpdateFlags is an enum in the C# driver that will let you specify both at once. Just like any other flags enum, you do this by bit "or"ing.

var flags = UpdateFlags.Upsert | UpdateFlags.Multi;

You can read the docs on enums here (http://msdn.microsoft.com/en-us/library/cc138362.aspx) paying special attention to the section on Enumeration Types as Bit Flags

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top