dannydwarren
dannydwarren

Reputation: 566

MongoDB Update Fails with No Errоr

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 (i.e. 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.

Sorry for the lack of explicit question: 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.

Upvotes: 3

Views: 4337

Answers (1)

dannydwarren
dannydwarren

Reputation: 566

Check the document (object) structure in the table and make sure it matches the structure you're sending to the db for update.

The Id of my updates were matching, but the JSON structure did not. This caused the update to fail silently. Not sure why that would be the case, but now that all documents are using the same structure edits work fine.

Old Document Structure (Update Failed):

{
    "_id": "54361fb2ab9c1e1b04514731",
    "Name": "New Name: 10/8/2014 10:40:03 PM",
    "HomeTown": "Carlsbad",
    "Ratings": [],
    "IsArchived": false,
    "PicturePath": "",
    "MatchWins": 0,
    "MatchLosses": 0,
    "GameWins": 0,
    "GameLosses": 0,
    "TotalGames": 0,
    "Matches": [],
    "WebcamImage": null
}

Current Document Structure (Update Succeed:

{
    "_id": {
        "$oid": "54362a35ab9c2025287025d2"
    },
    "_t": [
        "ModelBase",
        "Player"
    ],
    "Name": "Name Changed: 12/17/2014 8:58:23 PM",
    "HomeTown": "Carlsbad",
    "Ratings": [],
    "PicturePath": ""
}

So the Query matched a document, but the document structure did not match.

Upvotes: 1

Related Questions