Mongo 引擎无法执行原始查询
AppDocument.objects(__raw__={
{"_id": ObjectId("1"),"car._id": ObjectId("2")},
{"$pull":
{"car.$.toys": {"_id": ObjectId("3")}}
}
}
错误是:
TypeError: unhashable type: 'dict'
最佳答案
.objects()
仅用于查询,不用于更新。因此,__raw__
只能让您强制查询的filter
部分,而不是update
部分。
使用 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)
或者,请注意,您始终可以访问底层 pymongo 集合
coll = AppDocument._get_collection()
coll.update(find_qry, update_query)
关于python - MongoEngine 原始查询未解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57201418/