Problem to update subdocuments

56 views
Skip to first unread message

TM

unread,
Feb 17, 2012, 6:06:09 AM2/17/12
to mongodb-csharp
Hi,

I'm coding a update method for our obejcts and I have two issues where
you could really help me.

The first is an easy one: As I'm generating update statements at
runtime it can happen, that there is nothing to update. It would be
great, if you could add a property like IsEmpty on the UpdateBuilder,
so that I could break at this point. At the moment I'm using

if (updateBuilder.ToString() == "{ }") return;

and that is not so nice :)

The second thing is, that I'm trying to update subdocuments with the $-
operator.
When I call ToString() on the query and the update I'll get the
following:

Query : { "_id" : 39, "Contact._id" : 42 }
Update : { "$set" : { "Contact.$.Name" : "UpdatedContact " } }

.. but the update doesn't happen. When I call this update direct on
mongo shell everything works like expected.

Is this a bug or am I doing anything wrong?

Cheers

PS: I'm working with the latest sources from github.

Robert Stam

unread,
Feb 17, 2012, 11:49:09 AM2/17/12
to mongodb...@googlegroups.com
You could write the test for an empty Update like this:

if (updateBuilder.ToBsonDocument().ElementCount == 0)

it's slightly more characters to type but it's more efficient as it doesn't have to format a string, and more robust as you wouldn't be affected it the ToString representation ever changed (which it might, ToString is typically intended for human consumption).

On the second issue, the most likely cause the Update didn't happen is because the query didn't match any documents. Can you double check your element names and your values?
Reply all
Reply to author
Forward
0 new messages