C#中Up uped Mongo文档的ID

时间:2011-10-14 14:14:16

标签: c# mongodb upsert

如何在C#(Example)中从Insert到MongoDb集合获取返回Id已有详细记录。但是如何才能获得上传的文档呢?有没有办法,再次查询Id,使用Update返回的SafeModeResult来查找upserted文档Id?

My code:
var query = abc;
var update = xyz;
try
{
   db["Collection"].Update(query, update, UpdateFlags.Upsert);
}
catch
{
}

1 个答案:

答案 0 :(得分:0)

找到解决方案:

db["Collection"].FindAndModify(query, SortBy.Ascending(), update, true, true).Response.GetValue(1).AsBsonDocument.GetValue("_id")

返回ObjectId。