Partial mongodb upsert using the c# driver?

288 views
Skip to first unread message

Nik

unread,
Sep 24, 2011, 10:55:46 AM9/24/11
to mongodb-user
Hello.

I'm using Mongo 1.8.2 - the standard c# driver - quite a newbie.

Assume I have a class like

public class Acc{
public int _id { get; set; }
public int? Foo { get; set; }
public int? Bar{ get; set; }
}

Acc a = new Acc { _id = 1, Foo = 3};

I'd like to call

myCollection.Save(a),

such that

* if it doesn't exist, its inserted (easy so far)
* if it does exist, Foo is updated, but, but Bar remains whatever it
currently is (perhaps non-null...)

How do I achieve this partial upsert?

Many thanks.

Scott Hernandez

unread,
Sep 24, 2011, 11:45:22 AM9/24/11
to mongod...@googlegroups.com
Instead of doing a save (which is a full replacement by _id) you want
to only $set the field you want to update. This will leave the rest of
the existing data alone.

http://www.mongodb.org/display/DOCS/Updating#Updating-%24set
http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-Updatemethod

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages