0

I have to update in C# code using MongoDB. Here I had implement 2nd level array of update in below (subBranchindex is taken in a generic list object):-

for (var index = 0; index < subBranchindex.Count; index++)
{
    if (subBranchindex[index]._id == new ObjectId(subBranchid))
    {
        IMongoQuery queryEdit = Query.EQ("BranchOffice.SubBranchlist._id", new ObjectId(subBranchid));
        UpdateBuilder update = Update.Set("BranchOffice.$.SubBranchlist."+ index +".Name",subBranch.SubName).
        SafeModeResult s = dc.Collection.Update(queryEdit, update, 
        UpdateFlags.None, SafeMode.True);
    }
}

Here 2nd level array, I was using (for loop Statement) to taken Index value for array. Next I can use 3rd, 4th and 5th level of array means more than (for loop statement) will be assign. So don't need [for loop Statement] and also don't need to assign hardcore number in index. For example: ("BranchOffice.$.SubBranchlist.0.Name",subBranch.SubName). Here Don't Hardcore number[index] 0 or 1 or 2. "2nd" level array more than 100 record is there.

Is there any way I can use to array index value? Please explain how to solve this probelm. Please explain me with Example.

1
  • 1
    You might want to try feeding back on answers people give you or people may not bother to contribute in future. It looks like you're still solving related problems to this: stackoverflow.com/questions/12720655/… Commented Oct 13, 2012 at 17:57

1 Answer 1

0

Based on your example above, my understanding of your schema is the following:

  • The top-level document has a BranchOffice field
  • BranchOffice is an array of objects
  • Each object within BranchOffice has an _id, SubName and SubBranchlist field
  • SubBranchlist is an array of objects
  • Each object within SubBranchlist has a Name field

Your update statement appears to be copying the SubName field to each Name field among objects within SubBranchlist (a sibling field of SubName).

Using the property path syntax to select fields through arrays (e.g. SubBranchlist.0.Name), there is no "wildcard" index that will allow you to modify Name fields among all objects in the array.

On a somewhat related note, the $ positional operator only applies to the first-matched array element, so you cannot use that to update multiple array elements. In your case, it would not be an option anyway, since you're using the positional operator for the BranchOffice array field.

You can either issue a series of update queries (for each element in SubBranchlist), or consider using $set to modify the entire SubBranchlist array in one query. The downside with using $set is that you'll need to read and write back the entire array, which may be a problem if other, concurrent operations are also issuing updates to the array.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for help you, but The top-level document has a travelagent field. next level document [top-level document in one array field] has a Branchoffice. sorry for I didn't mention the top-level documents is travel agent.
In that case, your Update.Set() function may be incorrect; however, my points above still apply regarding your limits with $ and numeric positions in the field paths.

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.