python - Pull from a list in a dict using mongoengine -
i have document in mongo engine:
class mydoc(db.document): x = db.dictfield() item_number = intfield()
and have data document
{ "_id" : objectid("55e360cce725070909af4953"), "x" : { "mongo" : [ { "list" : "lista" }, { "list" : "listb" } ], "hello" : "world" }, "item_number" : 1 }
ok if want push mongo list using mongoengine, this:
mydoc.objects(item_number=1).update_one(push__x__mongo={"list" : "listc"})
that works pretty well, if query database again this
{ "_id" : objectid("55e360cce725070909af4953"), "x" : { "mongo" : [ { "list" : "lista" }, { "list" : "listb" }, { "list" : "listc" } ], "hello" : "world" }, "item_number" : 1 }
but when try pull same list using pull in mongo engine:
mydoc.objects(item_number=1).update_one(pull__x__mongo={'list': 'lista'})
i error:
mongoengine.errors.operationerror: update failed (cannot apply $pull non-array value)
comparising sentences:
mydoc.objects(item_number=1).update_one(push__x__mongo={"list" : "listc"}) # works mydoc.objects(item_number=1).update_one(pull__x__mongo={"list" : "listc"}) # error
how can pull list?
i appreciate help
i believe problem mongoengine doesn't know structure of x
document. declared dictfield
, mongoengine thinks pulling dictfield
not listfield
. declare x
listfield
, both queries should work fine.
i suggest should create issue this:
https://github.com/mongoengine/mongoengine/issues
as workaround, can use raw query:
mydoc.objects(item_number=1).update_one(__raw__={'$pull': {'x.mongo': {'list': 'listc'}}})
Comments
Post a Comment