1

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?

Community
  • 1
  • 1
Aafreen Sheikh
  • 4,949
  • 6
  • 33
  • 43
  • Dear downvoter, if you could so much as sacrifice 2 points of your reputation, you could spend 2 minutes to answer my question:-) If I was not clear, let me try again: I am asking how to specify both the upsert and multi flags in Update method of C#. I am not sure if the question is meaningless in some sense because the equivalent query does exist on the shell. – Aafreen Sheikh Jan 15 '13 at 18:16

1 Answers1

1

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

Craig Wilson
  • 12,174
  • 3
  • 41
  • 45