3

WHY IT HAPPENS

Because in certain cases upsert can't automatically generate an _id for the object you are manipulating.

SOLUTION A

Use [BsonIgnoreIfDefault] in the _id field of your model.

SOLUTION B

You can manually generate an _id before to upsert using ObjectId.GenerateNewId()

MORE INFO

Here and here.

ORIGINAL POST

Following the official documentation, I wrote this method. When i run it I receive this cryptic error message: "A bulk write operation resulted in one or more errors". Any idea what could be causing it? Everything looks fine. I already have bulk insert and remove methods working correctly.

public bool BulkUpsertReplaceOne (List<AppModel> records,
                                  string collectionName)
{
    try
    {
        var bulk = _database.
                   GetCollection(collectionName).
                   InitializeUnorderedBulkOperation();

        foreach(AppModel am in records)
        {
            IMongoQuery mongoQuery = Query.EQ ("Url", am.Url);
            bulk.Find (mongoQuery).Upsert().ReplaceOne(am);
        }
        bulk.Execute();
        return true;
    }
    catch(Exception e)
    {
        logger.Info (e.Message);
    }
}

EDIT COMPLETE ERROR

at MongoDB.Driver.Operations.BulkWriteBatchResultCombiner.CreateResultOrThrowIfHasErrors (IEnumerable`1 remainingRequests) in <filename unknown>:line 0
at MongoDB.Driver.Operations.BulkMixedWriteOperation.Execute (MongoDB.Driver.Internal.MongoConnection connection) in <filename unknown>:line 0
at MongoDB.Driver.MongoCollection.BulkWrite (MongoDB.Driver.BulkWriteArgs args) in <filename unknown>:line 0
at MongoDB.Driver.BulkWriteOperation.ExecuteHelper (MongoDB.Driver.WriteConcern writeConcern) in <filename unknown>:line 0
at MongoDB.Driver.BulkWriteOperation.Execute () in <filename unknown>:line 0
at SharedLibrary.Wrappers.MongoDB.MongoDBWrapper.BulkUpsertReplaceOne (System.Collections.Generic.List`1 records, System.String collectionName) in <filename unknown>:line 0"

EDIT2

I don't know if this is related, but if I check the database after the error, only ONE item of the bulk upsert have been inserted, and hasObjectId("000000000000000000000000").

Version: MongoDB 2.6.1, MongoC# driver 2.0.1

Community
  • 1
  • 1
Adrian Lopez
  • 2,601
  • 5
  • 31
  • 48
  • 1
    Look at the bulk `.Execute()` manual definition. It should return a detailed object containing the results of the write operation, including errors. Note that newer drivers have changed the behavior, as Unordered operations now throw an exception if any errors occur, even though the complete batch will be applied, with of course any errors reported. This was not consistently the case in previous versions, where the errors were recorded but not as an exception. – Blakes Seven Sep 16 '15 at 23:25
  • e.message shows "A bulk write operation resulted in one or more errors". Also, I can't access the result of .Execute() since it's interrupted by the error. I updated the post with the complete info of e. – Adrian Lopez Sep 16 '15 at 23:38
  • 1
    Can you just try to update one record instead of all, just to test if there is some bad data in one of the record, or there is an issue with Execute method. – Vlad Bezden Sep 16 '15 at 23:52
  • Ok, it's definitely an id problem. It's very weird because this problem didn't happened with my Bulk Insert and Bulk Remove methods. I'm going to update the post with the solucion. – Adrian Lopez Sep 17 '15 at 00:01
  • Do you have valid id in your AppModel record? – Vlad Bezden Sep 17 '15 at 00:01

1 Answers1

1

Looks like you have an issue with your AppModel's id record values. Looks like they are missing or all '000000000000000000000000'

Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179