Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb 3.6 arrayFilters expected a single top-level field name

Tags:

mongodb

Im trying to update nested array using arrayFilters, seems like it should work, according to documentation but I've got error:

Error parsing array filter: Expected a single top-level field name, found 'w' and 'd'

query looks like that:

db.getCollection("timesheets").update( {
     "year": "2018",
     "month": "06"    },    {
     "$set": {
       "workers.$[w].days.$[d]": {
         "day": 9,
         "month": "06",
         "year": "2018",
         "isHoliday": false,
         "isSaturday": true,
         "isSunday": false,
         "canEditPlanned": true
       }
     }    },   {
     "arrayFilters": [
       {

             "w._id": "XT5PFAPN9TFY3jXtJ",
             "d.day": 9

       }
     ]    })
like image 786
user2675118 Avatar asked Jun 26 '18 20:06

user2675118


1 Answers

When using arrayFilters to update a nested array, each nested level of the filter needs to be defined in its own element of the arrayFilters array:

db.getCollection("timesheets").update({
    "year": "2018",
    "month": "06"
  }, {
    "$set": {
      "workers.$[w].days.$[d]": {
        "day": 9,
        "month": "06",
        "year": "2018",
        "isHoliday": false,
        "isSaturday": true,
        "isSunday": false,
        "canEditPlanned": true
      }
    }
  }, {
    "arrayFilters": [
      {
         "w._id": "XT5PFAPN9TFY3jXtJ"
      },
      {
         "d.day": 9
      }
    ]
  })
like image 110
JohnnyHK Avatar answered Oct 16 '22 11:10

JohnnyHK