简体   繁体   English

使用MongoDb和C#更新嵌入式字段

[英]Update an Embedded Field using MongoDb and C#

I have this document 我有这个文件

    { "_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" } ] } ] }

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: 我尝试通过_id和Watchtbl.wid搜索并更新Watchtbl.name和Watchtbl.Symboles,所以我尝试仅以name开头,这是我的代码:

    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);

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. 而且,如果我尝试更新Watchtbl.Symboles,则必须执行两次相同的代码,没有其他方法可以同时更新所有代码。

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 但是我需要更新符号,然后出现此错误

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 严重性代码说明项目文件行抑制状态错误CS1660无法将lambda表达式转换为类型“ FieldDefinition”,因为它不是委托类型FinalWatchTbl C:\\ Users \\ amin- \\ Desktop \\ FinalWatchTbl \\ FinalWatchTbl \\ UpdateFrm.cs 117有效

Solution If I need to update a bson Array into nested document it work with 解决方案如果我需要将bson Array更新为嵌套文档,则可以使用

"Watchtbl.$.Symboles" so the solution is : “ Watchtbl。$。Symboles”,因此解决方案是:

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);

Lookup how to update documents in an array using the positional operator . 查找如何使用位置运算符更新数组中的文档

  // string id, int wid ...
  var fdb = Builders<BsonDocument>.Filter;
  var udb = Builders<BsonDocument>.Update;
  var filter = fdb.Eq("_id", id) & fdb.ElemMatch ("watchtbl", fdb.Eq ("wid", wid));
  var update = udb.Set("watchtbl.$.name", name);
  collectionWatchtbl.UpdateOne(filter, update);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM