0

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

1 Answer 1

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()
4
  • This Section is very helpful for me. I am using hierarchy in yours method "BrancheOffice.SubBranch._id"
    – Balaji
    Commented Oct 15, 2012 at 4:16
  • Cool. What else do you need to complete your answer?
    – cirrus
    Commented Oct 15, 2012 at 7:58
  • Thanks for help me. All things was completed in my application and also thanks for asking me. If I have any doubt means, I will ask you. Thank you very much.
    – Balaji
    Commented Oct 15, 2012 at 9:47
  • Would you mind ticking my answer as "accepted" if it solved your problem? many thanks.
    – cirrus
    Commented Oct 15, 2012 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.