'MongoEngine raw query not solved

Mongo engine cannot execute a raw query

AppDocument.objects(__raw__={
{"_id": ObjectId("1"),"car._id": ObjectId("2")}, 
            {"$pull": 
                {"car.$.toys": {"_id": ObjectId("3")}}
            }
}

The error is :

TypeError: unhashable type: 'dict'



Solution 1:[1]

.objects() is used only for querying, not updating. Thus, __raw__ only let you force the filter part of the query, not the update part.

The way you need to do that with mongoengine:

find_qry = {"_id": ObjectId("1"),"car._id": ObjectId("2")}
update_qry = {"$pull": {"car.$.toys": {"_id": ObjectId("3")}}}
AppDocument.objects(__raw__=find_qry).update(__raw__=update_qry)

Alternatively, note that you can always reach the underlying pymongo collection

coll = AppDocument._get_collection()
coll.update(find_qry, update_query)

Solution 2:[2]

You have to remove '{ }' extras

AppDocument.objects(__raw__=
{"_id": ObjectId("1"),"car._id": ObjectId("2")}, 
            {"$pull": 
                {"car.$.toys": {"_id": ObjectId("3")}}
            }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bagerard
Solution 2 ilkerv