Mihail
Mihail

Reputation: 395

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'

Upvotes: 2

Views: 3654

Answers (2)

ilkerv
ilkerv

Reputation: 1

You have to remove '{ }' extras

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

Upvotes: 0

bagerard
bagerard

Reputation: 6364

.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)

Upvotes: 2

Related Questions