update in a nested array using C# Driver in MongoD

2019-09-06 14:55发布

问题:

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

回答1:

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