【发布时间】:2021-06-04 12:33:42
【问题描述】:
var doc = new BsonDocument();
documentRepository.AddDocument(doc, "myCollection").GetAwaiter().GetResult();
public async Task<string> AddDocument(BsonDocument item, string collection)
{
await _database.GetCollection<BsonDocument>(collection).InsertOneAsync(item);
return item["_id"].ToString(); // this is where exception happens
}
E11000 重复键错误集合:myApp.myCollection 索引: id dup key: { _id: ObjectId('12381e2b09f14f0001fead43') } MongoDB.Driver.MongoWriteException: 写入操作导致 错误。
E11000 duplicate key error collection: myApp.myCollection index: _id_ dup key: { _id: ObjectId('12381e2b09f14f0001fead43') }
---> MongoDB.Driver.MongoBulkWriteException`1[MongoDB.Bson.BsonDocument]: A bulk write operation resulted in one or more errors.
E11000 duplicate key error collection: myApp.myCollection index: _id_ dup key: { _id: ObjectId('12381e2b09f14f0001fead43') }
at MongoDB.Driver.MongoCollectionImpl`1.BulkWriteAsync(IClientSessionHandle session, IEnumerable`1 requests, BulkWriteOptions options, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionBase`1.InsertOneAsync(TDocument document, InsertOneOptions options, Func`3 bulkWriteAsync)
混乱来自异常消息说我正在尝试执行批量插入这一事实
MongoDB.Driver.MongoBulkWriteException`1[MongoDB.Bson.BsonDocument]
我是否只插入一个文档,如您所见
【问题讨论】:
-
这是实际的 ObjectId 吗?
-
是的,它是.....