1

I have a collection looks like

{
 "Aid":12234,
 "items":{
   "itemId":"SP897474",
   "Blocks":[
     {
        "blockId":"W23456",
        "name":"B1",
        "innerBlock":[
           {
              "id":"S23490",
              "name":"IB1",
              "state":true
           },
           {
              "id":"S23491",
              "name":"IB2",
              "state":true
           },
           {
              "id":"S23492",
              "name":"IB3",
              "state":true
           }
        ],
        "active":true
     },
     {
        "blockId":"W23457",
        "name":"B2",
        "innerBlock":[
           {
              "id":"S23482",
              "name":"IB1",
              "state":true
           },
           {
              "id":"S23483",
              "name":"IB2",
              "state":true
           }
        ],
        "active":false
      }
   ]
 },
 "active":true
}

I'm not able to update fields that is of innerBlock array, specially name, status because of nesting. Basically, I wanted to update mentioned fields .Iam already try this query

User.update({
  "items.Blocks.innerBlock.id": req.body.id,
  "Aid": req.body.Aid
}, {
  "$set": {
    "items.Blocks.$.InnerBlock.$.name": req.body.name
  }
})

It shows an error given below

"errmsg": "Too many positional (i.e. '$') elements found in path 'items.Blocks.$.InnerBlock.$.name'"

I'm not sure how to fix it.What can be the query in mongo shell? Thanks.

5
  • Have you tried items.Blocks.InnerBlock.name Commented Jan 20, 2017 at 10:08
  • Yeah I tried it .It shows an error like this "errmsg": "cannot use the part (innerBlock of "items.Blocks.innerBlock.name) to traverse the element Commented Jan 20, 2017 at 10:15
  • check this answer out, I think it can help update nester array with mongoose Commented Jan 20, 2017 at 10:36
  • Try this "items.Blocks[0].InnerBlock[0].name": req.body.name Commented Jan 21, 2017 at 13:08
  • I found a solutions here jasonmciver.com/mongo-query-deep-array Commented Jan 23, 2017 at 10:37

1 Answer 1

0

Only one positional operator available right now with mongoDB. There are opened feature request for that https://jira.mongodb.org/browse/SERVER-831

check the answer https://stackoverflow.com/a/14855633/2066271

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

Comments

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.