I am getting consistent error: No array filter found for identifier 'elem' in path 'grades.$[elem] for sample code in Mongo Document: https://www.mongodb.com/docs/manual/reference/operator/update/positional-filtered/
db.students.insert_many( [
{
"_id" : 1,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 6 },
{ "grade" : 85, "mean" : 100, "std" : 4 },
{ "grade" : 85, "mean" : 100, "std" : 6 }
]
},
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 100, "std" : 6 },
{ "grade" : 87, "mean" : 100, "std" : 3 },
{ "grade" : 85, "mean" : 100, "std" : 4 }
]
}
] )
and the query:
cursor = db.students.update_many(
filter={ },
update={
'$inc' : {"grades.$[elem].std": -1},
'arrayFilters': [{"elem.grade": {'$gte': 80}, "elem.std": {'$gt': 5}}],
},
upsert=True
)
and the consistent error I get: pymongo.errors.WriteError: No array filter found for identifier 'elem' in path 'grades.$[elem].std'
the corect query need to look as follow:
db.students.update_many(
{},
{"$inc" : {"grades.$[elem].std" : -1}},
array_filters=[{"elem.grade":{"$gte" : 80 }, "elem.std":{"$lte" :5} }]
)
Explained: Considering usual update query consist of 3x sections:
The correct position of array_filters is not inside the update section , but afterwards in the options section.