使用MongoDB中的C#Driver在嵌套数组中进行更新 [英] update in a nested array using C# Driver in MongoDB

查看:1265
本文介绍了使用MongoDB中的C#Driver在嵌套数组中进行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的确切架构:

  {
_id:ObjectId(4fb4fd04b748611ca8da0d45),
Name:Agent name,
City:XXXX,
BranchOffice:[{

_id:ObjectId(4fb4fd04b748611ca8da0d46 ),
Name:Branch name,
City:XXXX,
SubBranch:[{

_id: ObjectId(4fb4fd04b748611ca8da0d47),
Name:分行名称,
City:XXXX
Users:[{

_id:ObjectId(4fb4fd04b748611ca8da0d48),
Name:User,
City:XXXX
}]
}]
}]
}

它已成功插入c#。



我想使用SubBranch和用户更新字段3级别和4级数组。



插入代码

  IMongoQuery query = Query .And(Query.EQ(_ id,new ObjectId(4fb4fd04b748611ca8da0d45)),

Query.EQ(BranchOffice._id,new ObjectId(4fb4fd04b748611ca8da0d46)));

代理代理= dc.Collection.FindOne(query);

BsonDocument branchOffice = agent.BranchOffice.Find(objId => objId._id == new ObjectId(4fb4fd04b748611ca8da0d46))。ToBsonDocument();

subBranch我有List对象转换为BsonDocument



文件:名称,城市,_Id和数组用户

  BsonDocument subBranchOffice = ** subBranch.ToBsonDocument *; 

if(branchOffice.Contains(SubBranch))
{
if(branchOffice [SubBranch]。IsBsonNull)
{
branchOffice [ SubBranch] = new BsonArray()。Add(BsonValue.Create(subBranchOffice));
}
else
{
branchOffice [SubBranch]。AsBsonArray.Add(BsonValue.Create(subBranchOffice));
}

var update = Update.Set(BranchOffice。$。SubBranch,branchOffice [SubBranch]);
SafeModeResult s = dc.Collection.Update(query,update,UpdateFlags.Upsert,SafeMode.True);
}

此处SafemodeResult为UpdateExisting = true >

此处插入选项已成功



下一步我尝试在else语句中更新。我不能得到答案



pre> else
{
var queryEdit = Query.And(Query.EQ(_ id,new ObjectId(4fb4fd04b748611ca8da0d45)),

Query.EQ(BranchOffice._id,new ObjectId(4fb4fd04b748611ca8da0d46)),
Query.EQ(SubBranchlist._id,new ObjectId(4fb4fd04b748611ca8da0d47)));

** //索引值1或2或3 **

var update = Update.Set(BranchOffice。$。SubBranch。index value.Name ,sname)。

Set(BranchOffice。$。SubBranch。index value.city,yyyyy?? string.Empty);
SafeModeResult s = dc.Collection.Update(queryEdit,update,UpdateFlags.None,SafeMode.True);
}

此处SafemodeResult为UpdateExisting = False >

此更新选项失败



请解释如何解决这个问题以及如何更新字段2和3的数组级别



请显示任何示例

解决方案

有很多,但它看起来像你的问题的至少部分是你已经拼写了BranchOffice不同的数据和你正在使用更新的查询,你已经错过了SubBranch的层次结构,所以你的queryEdit最后的代码示例将不匹配文档。这将;

  db.so.find({
_id:ObjectId(4fb4fd04b748611ca8da0d45),
BrancheOffice._id:ObjectId(4fb4fd04b748611ca8da0d46),
BrancheOffice.SubBranch._id:ObjectId(4fb4fd04b748611ca8da0d47),
})。toArray()


Here is my exact schema:

{
"_id" : ObjectId("4fb4fd04b748611ca8da0d45"),
"Name" : "Agent name",
"City" : "XXXX",
"BranchOffice" : [{

    "_id" : ObjectId("4fb4fd04b748611ca8da0d46"),
    "Name" : "Branch name",
    "City" : "XXXX",
    "SubBranch" : [{

        "_id" : ObjectId("4fb4fd04b748611ca8da0d47"),
        "Name" : "Sub-Branch Name",
        "City" : "XXXX"
        "Users" : [{

            "_id" : ObjectId("4fb4fd04b748611ca8da0d48"),
            "Name" : "User",
            "City" : "XXXX"
        }]
    }]
}]
}

Its Inserted successfully in c#. insert code was below but update condition is failed .

I want to update field 3 level and 4 level of array using SubBranch and users

Insert code

IMongoQuery query = Query.And(Query.EQ("_id", new ObjectId(4fb4fd04b748611ca8da0d45)),

Query.EQ("BranchOffice._id", new ObjectId(4fb4fd04b748611ca8da0d46)));

Agent agent = dc.Collection.FindOne(query);

BsonDocument branchOffice = agent.BranchOffice.Find(objId => objId._id ==    new ObjectId(4fb4fd04b748611ca8da0d46)).ToBsonDocument();

subBranch I had get List object convert to BsonDocument

Files: name,city,_Id, and users for array

BsonDocument subBranchOffice = **subBranch.ToBsonDocument()**;

if (branchOffice.Contains("SubBranch"))
{
    if (branchOffice["SubBranch"].IsBsonNull)
    {
        branchOffice["SubBranch"] = new BsonArray().Add(BsonValue.Create(subBranchOffice));
    }
    else
    {                                     
        branchOffice["SubBranch"].AsBsonArray.Add(BsonValue.Create(subBranchOffice));
    }

    var update = Update.Set("BranchOffice.$.SubBranch",branchOffice["SubBranch"]); 
    SafeModeResult s = dc.Collection.Update(query, update, UpdateFlags.Upsert,SafeMode.True);
}

Here SafemodeResult is UpdateExisting = true

Here Inserted Option is successfully

next I try to update in else Statement. I am not get it answer

Update code

else
{
    var queryEdit = Query.And(Query.EQ("_id", new ObjectId(4fb4fd04b748611ca8da0d45)), 

    Query.EQ("BranchOffice._id", new ObjectId(4fb4fd04b748611ca8da0d46)),
    Query.EQ("SubBranchlist._id", new ObjectId(4fb4fd04b748611ca8da0d47)));

    **//Index value 1 or 2 or 3**

    var update = Update.Set("BranchOffice.$.SubBranch."index value".Name", "sname").

    Set("BranchOffice.$.SubBranch."index value".city", "yyyyy" ?? string.Empty);
    SafeModeResult s = dc.Collection.Update(queryEdit, update, UpdateFlags.None,SafeMode.True);
}

Here SafemodeResult is UpdateExisting = False

Here updated Option is fail

Please explain how to solve this probelm and how to update field 2 and 3 level of array

Please show any Example

解决方案

There's a lot there, but it looks like at least part of your problem is that you've spelled BranchOffice differently between the data and the query you are using to update, also you've missed the hierarchy in SubBranch, so your queryEdit in the last code sample won't match the document. This will;

db.so.find({
    _id: ObjectId("4fb4fd04b748611ca8da0d45"),
    "BrancheOffice._id": ObjectId("4fb4fd04b748611ca8da0d46"),
    "BrancheOffice.SubBranch._id": ObjectId("4fb4fd04b748611ca8da0d47"),
}).toArray()

这篇关于使用MongoDB中的C#Driver在嵌套数组中进行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆