如何使用新的C#API更新mongodb中的数组。 例如,如何修改medecins:
{
"_id" : {
"periode" : "Matin",
"jour" : "Dimanche",
"service" : "Neurologie"
},
"medecins" : [
ObjectId("574ec788572e1d2d20f9c7e2"),
ObjectId("57471e2f572e1d366c056735")
]
}
这是我的代码不起作用:
ObjectId[] id_medecins = new ObjectId[IdMedecins.list.Count];
for(int i=0; i<IdMedecins.list.Count;i++)
{
id_medecins[i] = ObjectId.Parse(IdMedecins.list[i].ToString());
}
MongoClient client = Model.connectionServer();
var db = client.GetDatabase("GP");
var collection = db.GetCollection<BsonDocument>("horaire");
var filter = Builders<BsonDocument>.Filter.Eq("_id", horaire_);
var update = Builders<BsonDocument>.Update
.Set("medecins",id_medecins);
var result = collection.UpdateOneAsync(filter, update);
var resultMsg = result.Result.MatchedCount.ToString();
if (result.Result.IsModifiedCountAvailable)
{
MessageBox.Show("Modifié avec succès", "message", MessageBoxButtons.OK, MessageBoxIcon.Information);
Close();
}