Im trying to do a mongodb query that match some nested array conditions without success...
Documents:
[
{
"_id":1,
"name":"foo",
"games":[
{
"name":"Game1",
"data":[
{ "data_id":1, "date":"YYYMMDD" },
{ "data_id":2, "date":"YYYMMDD" }
]
},
{
"name":"Game2",
"data":[
{ "data_id":1, "date":"YYYMMDD" },
{ "data_id":2, "date":"YYYMMDD" }
]
}
]
},
{
"_id":2,
"name":"bar",
"games":[
{
"name":"Game2",
"data":[
{ "data_id":1, "date":"YYYMMDD" },
{ "data_id":2, "date":"YYYMMDD" }
]
}
]
}
]
And i want to do an update_many, matching the following data:
all documents
AND ( (_id = 1 and games.name = 'Game1' and games.data.data_id = 1 ) OR (_id = 2 and games.name = 'Game2' and games.data.data_id = 1 ) )
set valid = True
So, for each document i want to update a specific games.data.data_id element.
What i tried
query = {}
update = {"$set": {"games.$[i].data.$[j].valid": True}}
array_filters = [{'$or': [{'_id': 1, 'i.name': 'Game1', 'j.data_id': 1}, {'_id': 2, 'i.name': 'Game2', 'j.data_id': 1}]}]
But it returns an error:
WriteError: Error parsing array filter :: caused by :: Expected a single top-level field name, found 'i' and 'j'
The expected output is:
[
{
"_id":1,
"name":"foo",
"games":[
{
"name":"Game1",
"data":[
{ "data_id":1, "date":"YYYMMDD", "valid": True },
{ "data_id":2, "date":"YYYMMDD" }
]
},
{
"name":"Game2",
"data":[
{ "data_id":1, "date":"YYYMMDD" },
{ "data_id":2, "date":"YYYMMDD" }
]
}
]
},
{
"_id":2,
"name":"bar",
"games":[
{
"name":"Game2",
"data":[
{ "data_id":1, "date":"YYYMMDD", "valid": True },
{ "data_id":2, "date":"YYYMMDD" }
]
}
]
}
]
So, what am i doing wrong? How to do this? I have tried several other choices, like combining $or and $and, but every time that i use multiple identifiers on same condition, it fails