如何编写此更新查询以更新c#中的mongo记录.
db.collection.update({ "S_Id" : 110 },{ "Name" : "Name1","Batch" : "43","Date":"9/2/2011", "Status" : 0 });
Run Code Online (Sandbox Code Playgroud)
我是这样想的
IMongoUpdate update = new UpdateDocument();
if (Named != null) { update = Update.Set("Name", "Name1"); }
if (Date != null) { update = Update.Set("Date", "18/02/2013"); }
if (Batch != null) { update = Update.Set("Batch",43); }
coll.Update(query, update);
Run Code Online (Sandbox Code Playgroud)
我做得正确或以何种方式做到这一点,请让我以正确的方式继续下去.
小智 13
在您的示例中,您可能会覆盖update每个选项的值,因此只会发送一个更新命令col1.Update().
你会想要使用这个Update.Combine方法,有点像这样:(未经测试,有点难看......)
var updateValues = new List<UpdateBuilder>();
if (Named != null) { updateValues.Add(Update.Set("Name", "Name1")); }
if (Date != null) { updateValues.Add(Update.Set("Date", "18/02/2013")); }
if (Batch != null) { updateValues.Add(Update.Set("Batch", 43)); }
IMongoUpdate update = Update.Combine(updateValues);
coll.Update(query, update);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9147 次 |
| 最近记录: |