Search code examples
pythondjangomongodbbackendpymongo

{'index': 0, 'code': 2, 'errmsg': "No array filter found for identifier 'i' in path 'Comments.$[i].Replies.$[j].Reply'"}


enter image description here

WriteError: No array filter found for identifier 'i' in path 'Comments.$[i].Replies.$[j].Reply', full error: {'index': 0, 'code': 2, 'errmsg': "No array filter found for identifier 'i' in path 'Comments.$[i].Replies.$[j].Reply'"}

I just want to update the reply in my data. I'm getting this error. Is there any other way to update?

Code

article_id = "6223bf189ee543673ca35940"
comment_id = "2d1ae2a7-1488-44b0-8ba3-9e946ed8cca9"
reply_id = "c0b9d54a-416f-4598-bf7b-33be80faa5c3"


article_collection.update_one({'_id': ObjectId(article_id)}, 
            {
                '$set': {'Comments.$[i].Replies.$[j].Reply': reply},
                'arrayFilters': [{'i.ID': comment_id}, {'j.ID': reply_id}]
            }, upsert=False)

Data

{
    "_id" : ObjectId("6223bf189ee543673ca35940"),
    "Comments" : [ 
        {
            "ID" : "2d1ae2a7-1488-44b0-8ba3-9e946ed8cca9",
            "User_id" : 1,
            "Comment" : "Thank you for this simple explanation.",
            "Date" : ISODate("2022-03-13T02:13:09.022Z"),
            "Replies" : [ 
                {
                    "ID" : "c0b9d54a-416f-4598-bf7b-33be80faa5c3",
                    "User_id" : 1,
                    "Reply" : "You're welcome",
                    "Date" : ISODate("2022-03-13T12:53:39.046Z")
                }
            ]
        }
    ]
}

Solution

  • Error message correctly suggest the arrayFilters are not found. You have syntax error, arrayFilters need to be added in the options section of your update query , currently is in the update section:

    Update({query},{update},{options}) , the correct one:

     article_collection.update_one({'_id': ObjectId(article_id)}, 
            {
                '$set': {'Comments.$[i].Replies.$[j].Reply': reply}}, 
            {
                'arrayFilters': [{'i.ID': comment_id}, {'j.ID': reply_id}]
            }, upsert=False)