带有C#driver 2.0的MongoDB(服务器V2.6.7):如何从InsertOneAsync获取结果
我正在用C#driver 2.0测试MongoDB(服务器V2.6.7) 当我对存在带有C#driver 2.0的MongoDB(服务器V2.6.7):如何从InsertOneAsync获取结果,c#,mongodb,async-await,mongodb-.net-driver,C#,Mongodb,Async Await,Mongodb .net Driver,我正在用C#driver 2.0测试MongoDB(服务器V2.6.7) 当我对存在\u id的文档使用insert函数InsertOneAsync时,我预期会出现类似您从Mongo shell获得的错误: 但问题是,带有C#驱动程序的insert没有抛出异常,我无法找到insert的WriteResult。 当我查看数据库时,似乎什么都没有发生 因此,我的问题是,当插入现有的\u id时,InsertOneAsync会带来什么 Visual Studio中的代码: IMongoCollecti
\u id
的文档使用insert函数InsertOneAsync
时,我预期会出现类似您从Mongo shell获得的错误:
但问题是,带有C#驱动程序的insert没有抛出异常,我无法找到insert的WriteResult
。
当我查看数据库时,似乎什么都没有发生
因此,我的问题是,当插入现有的\u id
时,InsertOneAsync
会带来什么
Visual Studio中的代码:
IMongoCollection<BsonDocument> commandsCollection = db.GetCollection<BsonDocument>("Commands");
var bson = new BsonDocument
{
{"_id", i.Value},
{"label", i.Key}
};
commandsCollection.InsertOneAsync(bson);
IMongoCollection命令collection=db.GetCollection(“命令”);
var bson=新的BsonDocument
{
{“_id”,i.Value},
{“标签”,i.Key}
};
commandsCollection.InsertOneAsync(bson);
这是一项异步任务,您错过了等待
await commandsCollection.InsertOneAsync(bson);
如果您在
异步方法中执行此操作,则Brduca的答案将有效(且更可取),否则您可以调用InsertOneAsync
调用返回的任务,以确保应用程序停留足够长的时间以查看重复密钥异常:
commandsCollection.InsertOneAsync(doc).Wait();
如果插入因重复键而失败,则Wait()
将抛出一个aggregateeexception
,该异常包含包含重复键详细信息的MongoWriteException
try
{
commandsCollection.InsertOneAsync(doc).Wait();
}
catch(AggregateException aggEx)
{
aggEx.Handle(x =>
{
var mwx = x as MongoWriteException;
if (mwx != null && mwx.WriteError.Category == ServerErrorCategory.DuplicateKey)
{
// mwx.WriteError.Message contains the duplicate key error message
return true;
}
return false;
});
}
类似地,如果您使用的是await
,那么也会抛出一个aggregateeexception
为了避免包装mongo异常的AggregateException
增加了复杂性,您可以调用GetWaiter().GetResult()
而不是Wait()
:
进一步到@JonnyHK reply,您可以在插入多个时执行相同的操作
collection.InsertManyAsync(doc, new InsertManyOptions { IsOrdered = false }).Wait();
将被包裹在try/catch中
try
{
collection.InsertManyAsync(doc, new InsertManyOptions { IsOrdered = false }).Wait();
}
catch (AggregateException aggEx)
{
aggEx.Handle(x =>
{
var mwx = x as MongoBulkWriteException;
return mwx != null && mwx.WriteErrors.All(e => e.Category == ServerErrorCategory.DuplicateKey);
});
}
好的,我添加了wait,但是现在C#程序只是在插入的地方安静地终止。我想在我面前抛出一个异常^^您是否尝试显式保存集合?commandsCollection.Save()不幸的是,新版本的C#驱动程序中不存在这样的命令。第二个响应,同步时是否有效?你的代码似乎还可以。可能是连接到服务器的问题。是的,或者我不知道发生了什么。因为我插入了完全相同的值,所以我无法确定其中的值是“新”值还是“旧”值。我似乎找不到任何关于异步方法的好文档,但是如何检测重复的密钥呢?InsertOneAsync将返回一个没有结果部分的任务。等待将返回一个空白。谢谢。还有一件事。如何在MongoWriteException中找到重复的密钥信息catch(aggregateeexception e){e.Handle((x)=>{if(x是MongoWriteException){}返回true;})
如果抛出MongoWriteException,并且异常参数名为mwx,mwx是否可能为null?换句话说,mwx!=null检查真的有必要吗?@develDog74良好捕获-已编辑。这是从第一个需要它的实现中遗留下来的。wait
不会抛出aggregateeException
。它将引发实际的异常。
collection.InsertManyAsync(doc, new InsertManyOptions { IsOrdered = false }).Wait();
try
{
collection.InsertManyAsync(doc, new InsertManyOptions { IsOrdered = false }).Wait();
}
catch (AggregateException aggEx)
{
aggEx.Handle(x =>
{
var mwx = x as MongoBulkWriteException;
return mwx != null && mwx.WriteErrors.All(e => e.Category == ServerErrorCategory.DuplicateKey);
});
}