Mongodb update not working without errors

I am using latest MongoDB-C # driver in ASP.NET MVC WebAPI controller.

I don't get the error:

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

      

But the reported number of affected documents is 0.

If I run UnitTest directly against the controller (i.e. it is not hosted or running on any server, just treating it as a normal class, instantiating the controller in my UnitTest), when I do this, everything works as expected.

So I have this problem 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, then it succeeds.

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

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

      

And then I modify this object and call a message 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 will be done.

Thanks for the help!

UPDATE Sorry for not having a clear question: am I doing something wrong? Any ideas for figuring out what's going on with the real flop? Is there a way to debug MongoDB calls?

More information. We are using MongoLab in Azure to do our MongoDB relay.

+3


source to share


1 answer


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

The ID of my updates was mapped, but the JSON structure didn't. This caused the update to fail. Not sure why that would be the case, but now that all documents use the same structure changes, works great.

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
}

      



Structure of the current document (Update Succeed:

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

      

So the request matched the document, but the structure of the document did not match.

+1


source







All Articles