Update all fields but one

1,391 views
Skip to first unread message

Senor Cardgage

unread,
Aug 9, 2011, 5:51:31 PM8/9/11
to mongodb-csharp
I have a class that I update in the data store like this:

public void Update()
{
MongoServer srv = MongoServer.Create(connStr);

var update = MongoDB.Driver.Builders.Update
.Set("Name", Name)
.Set("Type", Type)
... //lots and lots of calls to Set() here

srv[dbName][colletion].Update(Query.EQ("_id", Id),
update);
}

The update is built using all but one of the properties of the class.
I am only excluding a single property from the update. Is there a
more concise way to represent this update where I indicate which
properties to exclude rather than which properties to include?

Robert Stam

unread,
Aug 9, 2011, 6:22:12 PM8/9/11
to mongodb-csharp
If you are changing practically the whole document it would probably
be easier to change the document client side and call Save instead of
Update (or Update with Update.Replace).

Senor Cardgage

unread,
Aug 9, 2011, 6:26:51 PM8/9/11
to mongodb-csharp
The one field that I do not want to update is a relatively large
subdocument that I don't keep in memory. So I don't want to pull it
in, just so I can save it back out. I can live with what I've got if
there isn't a more concise way to do what I want.

Robert Stam

unread,
Aug 9, 2011, 6:31:22 PM8/9/11
to mongodb-csharp
You could compute an UpdateDocument instead of listing all the fields
individually using the Update builder, but that might not be worth the
trouble.
Reply all
Reply to author
Forward
0 new messages