0

I´m executing the UpdateMany method of MongoDb collection, the result is always zero updated registers.

I´m using MondoDb.Driver for C# (Version 2.5.0.0)

This is my code that execute a update function by a filter. In my test I find 2 elements and update two elements that I can check it:

public bool UpdateFields(FilterDefinition<BsonDocument> filter, UpdateDefinition<BsonDocument> update)
    {
        if (dbMongoCollection == null)
        {
            dbMongoCollection = CreateStandardMongoDbClient();
        }

        var result = dbMongoCollection.UpdateMany(filter, update);

        if (result.IsAcknowledged)
        {
            return result.MatchedCount > 0 && result.ModifiedCount > 0;
        }
        return false;
    }

Curiously, it always return this values:

result.IsAcknowledged   true    bool
result.IsModifiedCountAvailable true    bool
result.MatchedCount 0   long
result.ModifiedCount    0   long

I´d like to know the number of modified/updated elements.

I will appreciate assistance.

3
  • result.MatchedCount 0 indicates that no documents were found that match the provided filter. Make sure dbMongoCollection is pointing to the right database and collection. Perhaps use profiling to get the query received by the server to compare with what you intended to send.
    – Joe
    Aug 20, 2018 at 1:33
  • The query updated 2 registers, but return MatchedCount 0. I tried installing the last version of MongoDB nuget driver. Oct 9, 2018 at 8:43
  • The code you've provided looks fine. Could you put together some example code that starts with an empty database and demonstrates the problem?
    – Joe
    Nov 9, 2018 at 23:45

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.