I have a nested array that I would like to add to, but I'm having difficulty. My goal is to append to this array if it does not exist, and if it does, it will change the information within it.
For example, I'd like to edit the content of a dictionary within 'arr4' with the identification ('id4') of 'foo' from '...' to '!!!'.
This is setup with PyMongo here:
diction = {
"arr1": [
{
"id1": "abc",
"someInfo1": "...",
"someInfo2": "...",
"someInfo3": "...",
"someInfo4": "...",
"arr2": [
{
"id2": "ijk",
"arr3": [
{
"id3": "xyz",
"someInfo1": "...",
"someInfo2": "...",
"arr4": [
{
"id4": "foo",
"someInfo": "..."
},
{
"id4": "bar",
"someInfo": "..."
}
]
}
]
}
]
}
]
}
import pymongo
from pymongo import MongoClient
client = MongoClient()
db = client['pymongo_test2']
db.posts.insert_one(diction)
In order to access these arrays, I think I would use array filters, so I tried
id1 = 'abc'
id2 = 'ijk'
id3 = 'xyz'
id4 = 'foo'
db.posts.update_one({}, {'$set': {'arr1.$[element1].arr2.$[element2].arr3.$[elemenet3].arr4': {'id4': 'foo', 'someInfo': '!!!'}, 'arrayFilters': {'element1.id1': id1, 'element2.id2': id2, 'elemenet3.id3': id3}}}, True)
but I get the error:
pymongo.errors.WriteError: No array filter found for identifier 'element1' in path 'arr1.$[element1].arr2.$[element2].arr3.$[elemenet3]'
Any help is greatly appreciated!