Mongodb Update Fails with No Error

559 views
Skip to first unread message

Danny Warren

unread,
Oct 15, 2014, 9:53:14 PM10/15/14
to mongod...@googlegroups.com

Mongo Experts!

I need your help to figure out what I can do to get my updates to succeed. Please see my problem below (excerpt from my StackOverflow question):

QUESTION:

I'm using the latest MongoDB-C# Driver in an ASP.NET MVC WebAPI controller.

I do not receive an error when I call:

collection.Update(Query<T>.EQ(e => e.Id, entity.Id), 
    MongoDB.Driver.Builders.Update<T>.Replace(entity), WriteConcern.Acknowledged)

But the reported number of documents affected is 0.

If I run a UnitTest directly against the controller (ie. It's not hosted or running under any server. Just treating it like a normal class by instantiating the controller in my UnitTest) when I do this then everything works as expected.

So I only have this issue while my site is hosted.

MORE: If I create a new object and then try to modify that created object and call the above code than the update succeeds.

Example: If I call Post on my WebAPI controller to create an object which runs this code:

MongoConnection.Collection.Insert(value, WriteConcern.Acknowledged)

And then I modify that object and call Post on my WebAPI controller which runs the first block of code:

collection.Update(Query<T>.EQ(e => e.Id, entity.Id), 
    MongoDB.Driver.Builders.Update<T>.Replace(entity), WriteConcern.Acknowledged)

Then the update succeeds.

Am I doing something wrong? Any ideas for figuring out what the actual failure is? Is there a way to debug the calls to MongoDB?

One more piece of information. We are using MongoLab in Azure to host our MongoDB repo.

Thanks in advanced for the help!


-Danny

Gianfranco

unread,
Oct 20, 2014, 11:08:35 AM10/20/14
to mongod...@googlegroups.com
Hi Danny,

When an update operation does not find any documents based on the query part it will not update anything, but it will still return something.

What does it return in your case?
In 2.6 it returns "nModified" with the number of documents updated.

You can check that to see if the update operation succeeds.

See this example from the mongo shell:

Cheers,
Gianfranco
Reply all
Reply to author
Forward
0 new messages