Inserting data to nested array in mongodb

后端 未结 3 614
青春惊慌失措
青春惊慌失措 2020-12-07 00:24

I have a document which looks like this

{
 _id:1,
 list_id:23,
 name:\'list01\'
 cards:[
  {
   id:3,
   name:\'card01\'
   categories:[{
    id:10,
    categ         


        
3条回答
  •  春和景丽
    2020-12-07 00:57

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

    The filtered positional operator $[] 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

提交回复
热议问题