2

I have a document which looks like this

{
 _id:1,
 list_id:23,
 name:'list01'
 cards:[
  {
   id:3,
   name:'card01'
   categories:[{
    id:10,
    category:'section01',
    tags:[{id:11,name:'tag01',is_selected: true}]
  }] 
  }
 ]
}

I need to insert/push some data to tags array in a selected category for a given list_id but I'm getting an error saying

MongoError: Too many positional (i.e. '$') elements found in path 'cards.$.categories.$.tags'

This is the query that I have tried out. What's wrong with this query any idea on how to achieve this?

db.collection(TABLE)
    .updateOne(
      { list_id: 23, 'cards.categories.category': 'section01'},
      { $push: { 'cards.$.categories.$.tags': { name: 'tag02', id: uuidv4(), is_selected: true } } }
    );
1
  • can not use multiple $ positional, use arrayFilters, Commented Sep 28, 2020 at 6:57

3 Answers 3

2

You can not use multiple $ positional, for your case you can use single positional and arrayFilters,

The filtered positional operator $[<identifier>] identifies the array elements that match the arrayFilters conditions for an update operation,

db.collection(TABLE).updateOne({
  list_id: 23,
  "cards.categories.category": "section01"
},
{
  $push: {
    "cards.$.categories.$[elem].tags": {
      name: "tag02",
      id: uuidv4(),
      is_selected: true
    }
  }
},
{
  arrayFilters: [
    { "elem.category": "section01" }
  ]
})

Playground

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

Comments

2

You can use $[identifier]

db.collection.update({
  "list_id": 23,
  "cards.categories.category": "section01"
},
{
  $push: {
    "cards.$.categories.$[elem].tags": {
      name: "tag02",
      id: uuidv4(),
      is_selected: true
    }
  }
},
{
  arrayFilters: [
    {
      "elem.category": "section01"
    }
  ],
  multi: true
})

try it here

Comments

1

In short, it is not possible.

Nested Arrays

The positional $ operator cannot be used for queries which traverse more than one array, such as queries that traverse arrays nested within other arrays, because the replacement for the $ placeholder is a single value

https://docs.mongodb.com/manual/reference/operator/update/positional/

However, you may want to try $[]

Nested Arrays The filtered positional operator $[] can be used for queries which traverse more than one array and nested arrays.

For an example, see Update Nested Arrays in Conjunction with $[].

https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/#position-nested-arrays-filtered

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.