使用MongoDb和C#更新嵌入式字段 [英] Update an Embedded Field using MongoDb and C#
问题描述
我有这份文件
{ "_id" : ObjectId("57f65ed25ced690b5408a9d1"), "fbId" : "7854", "Name" : "user1", "pass" : "user1", "Watchtbl" : [ { "wid" : "745", "name" : "azs", "Symboles" : [ { "Name" : "nbv" } ] }, { "wid" : "8965", "name" : "bought stock1", "Symboles" : [ { "Name" : "AAA" }, { "Name" : "BSI" }, { "Name" : "EXXI" }, { "Name" : "AMD" } ] }, { "wid" : "9632", "name" : "bought stock3", "Symboles" : [ { "Name" : "AAA" }, { "Name" : "AMD" } ] } ] }
我尝试通过_id和Watchtbl.wid进行搜索并更新Watchtbl.name和Watchtbl.Symboles,所以我尝试仅以名称开头,这是我的代码:
I try to search by _id and Watchtbl.wid and update the Watchtbl.name and Watchtbl.Symboles , so I try just to start with name, this my code:
var collectionWatchtbl = _database.GetCollection<BsonDocument>("UsersWatchtbls");
var filter = Builders<BsonDocument>.Filter.Eq("_id", id) & Builders<BsonDocument>.Filter.Eq("Watchtbl.wid", wid );
var update = Builders<BsonDocument>.Update.Set("Watchtbl.name", NameComboBox.SelectedItem.ToString());
var result = await collectionWatchtbl.UpdateOneAsync(filter, update);
但是没有任何反应,即使没有错误. 而且比起我尝试更新Watchtbl.Symboles,必须两次执行相同的代码,没有其他方法可以同时更新所有内容.
But nothing happen, even there is no error. and than if I try to update Watchtbl.Symboles, must I do the same code twice, there is not another way to update all at the same time.
有1个项目需要更新的解决方案
Solution with 1 item to update
BsonArray arrSym = new BsonArray();
foreach (var item in SymbolesListBox.SelectedItems)
{
arrSym.Add(new BsonDocument("Name", item.ToString()));
}
var filter = Builders<UserWatchTblCls>.Filter.Where(x=> x.Id == ObjectId.Parse(id) && x.WatchTbls.Any(i=> i.WID == wid) );
var update = Builders<UserWatchTblCls>.Update.Set(x=> x.WatchTbls[-1].Name, NameComboBox.SelectedItem.ToString()).Set(x => x.WatchTbls[-1].Symbols, arrSym);
await collectionWatchtbl.UpdateManyAsync(filter, update);
如果我删除了这部分,它将起作用并更新名称,
If I remove this part, will work and update the name,
.Set(x => x.WatchTbls[-1].Symbols, arrSym)
但是我需要更新符号,然后出现此错误
But I need to update the Symboles, and I get this error
严重性代码描述项目文件行抑制状态 错误CS1660无法将Lambda表达式转换为类型'FieldDefinition',因为它不是委托类型FinalWatchTbl C:\ Users \ amin- \ Desktop \ FinalWatchTbl \ FinalWatchTbl \ UpdateFrm.cs 117有效
Severity Code Description Project File Line Suppression State Error CS1660 Cannot convert lambda expression to type 'FieldDefinition' because it is not a delegate type FinalWatchTbl C:\Users\amin-\Desktop\FinalWatchTbl\FinalWatchTbl\UpdateFrm.cs 117 Active
解决方案 如果我需要将bson数组更新为嵌套文档,则可以使用
Solution If I need to update a bson Array into nested document it work with
"Watchtbl.$.Symboles" 所以解决方法是:
"Watchtbl.$.Symboles" so the solution is :
BsonArray arrSym = new BsonArray();
var filter = Builders<UserWatchTblCls>.Filter.Where(x => x.Id == ObjectId.Parse(id) && x.WatchTbls.Any(i => i.WID == wid));
var update = Builders<UserWatchTblCls>.Update.Set(x => x.WatchTbls[-1].Name, NameComboBox.SelectedItem.ToString()).Set("Watchtbl.$.Symboles", arrSym);
//var update = Builders<UserWatchTblCls>.Update.Set("Watchtbl.$.Symboles", arrSym);
await collectionWatchtbl.UpdateManyAsync(filter, update);
推荐答案
查找方法
这篇关于使用MongoDb和C#更新嵌入式字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!