如何在查询中使用 objectId .net

how to use objectId in a query .net

我需要在不同的文档 (MongoDB) 中插入(或创建)一个字段。
那是我的代码:

var myDB = mclient.GetDatabase("MasterDB"); 
var collection = myDB.GetCollection<BsonDocument>("MasterCollection");
var list = await collection.Find(new BsonDocument())
                           .Limit(2) 
                           .Skip(1)
                           .ToListAsync();

foreach (var docs in list)
{               
    var updoneresult1 = await collection.UpdateManyAsync(
    Builders<BsonDocument>.Filter.Eq("_id", docs["_id"].ToString()),
    Builders<BsonDocument>.Update.Push("OfficeCode", 
    Convert.ToInt32(txtOfficeCode.Text)));     
}

我认为您需要添加: http://api.mongodb.com/csharp/current/html/M_MongoDB_Bson_BsonDocument_Add_2.htm

foreach (var docs in list)
{ 
    ....          
    docs.Add("OfficeCode", Convert.ToInt32(txtOfficeCode.Text));
    ....
}

PS我真的不知道你想在这里完成什么。

编辑:

在评论中您说您无法通过 _id 匹配它。尝试改变

docs["_id"].ToString()

ObjectId.Parse(docs["_id"].ToString())