I have the following doc structure (simplified) in mongo:
{
"_id":"5e30208675b5400cb0894c52",
"locations": [
{
"name": "Pleasure Gardens",
"id": 618,
"areas": [
{
"name": "Koi Pond",
"area_id": 159,
"is_active": true,
},
...other areas
],
...other locations
}
]
}
I'm trying to set the is_active field in the areas array to false where the id of the location is 618 and the the area_id of the area is 159.
I'm doing the following:
const db = await connectDb();
return await db
.collection('test')
.updateOne(
{"locations.areas.area_id": 159},
{ $set: {"locations.$[l].areas.$[a].is_active": false }},
{ arrayFilters: [{ "l.id": 618 }, { "a.area_id": 159 }] }
);
But I'm getting:
MongoError: cannot use the part (locations of locations.$[l].areas.$[a].is_active) to traverse the element
I've checked other responses and as far as I can tell the syntax is correct and I'm also using a mongodb version that supports the arrayFilters method ("mongodb": "^3.6.3") in nodejs, so why is it being so cruel to me? WHYYYY?!?!
UPDATE
After reading through the comments I realised that although I have version 3.6.3 of the mongodb npm package installed, the version of the database itself is actually 3.0.14
This means that the arrayFilters method is not supported and I will probably have to take a 2 step approach to solve this problem:
- query mongodb to find the document:
- store result in a variable
- update the variable in the code itself with (change is_active to false)
- use an update query to substitute the whole document in mongo with the document stored in variable (rather than just updating the desired area)