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:
For me is working correctly(mongod/mongos version 4.4.3):
mongos> db.l.find().pretty()
{
"_id" : "5e30208675b5400cb0894c52",
"locations" : [
{
"name" : "Pleasure Gardens",
"id" : 618,
"areas" : [
{
"name" : "Koi Pond",
"area_id" : 159,
"is_active" : true
}
]
}
]
}
mongos> db.l.updateOne( {"locations.areas.area_id": 159} , { $set:{"locations.$[loc].areas.$[are].is_active":false }}, { arrayFilters: [{ "loc.id": 618 }, { "are.area_id": 159 }] } )
// { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
mongos> db.l.find({ "locations.areas.area_id": 159 } ,{"locations.areas.is_active":1 })
// { "_id" : "5e30208675b5400cb0894c52", "locations" : [ { "areas" : [ { "is_active" : false } ] } ] }