Pull from a list in a dict using mongoengine

I have this document in the mongo engine:

class Mydoc(db.Document): x = db.DictField() item_number = IntField() 

And I have this data in the document

 { "_id" : ObjectId("55e360cce725070909af4953"), "x" : { "mongo" : [ { "list" : "lista" }, { "list" : "listb" } ], "hello" : "world" }, "item_number" : 1 } 

Well, if I want to click on a mongo list using mongoengine, I do this:

 Mydoc.objects(item_number=1).update_one(push__x__mongo={"list" : "listc"}) 

This works very well if the database query again I get this

 { "_id" : ObjectId("55e360cce725070909af4953"), "x" : { "mongo" : [ { "list" : "lista" }, { "list" : "listb" }, { "list" : "listc" } ], "hello" : "world" }, "item_number" : 1 } 

But when I try to pull from the same list using pull in mongo engine:

 Mydoc.objects(item_number=1).update_one(pull__x__mongo={'list': 'lista'}) 

I get this error:

mongoengine.errors.OperationError: update failed ($ pull cannot be applied to value without array)

comparison of offers:

 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 I extract from this list?

I appreciate any help

+5
source share
1 answer

I believe the problem is that mongoengine does not know the structure of your x document. You declared it as a DictField , so mongoengine thinks that you are DictField from a DictField not from a ListField . Declare x as a ListField and both queries should work fine.

I suggest you also create a problem for this:
https://github.com/MongoEngine/mongoengine/issues

As a workaround, you can use a raw request:

 Mydoc.objects(item_number=1).update_one(__raw__={'$pull': {'x.mongo': {'list': 'listc'}}}) 
+1
source

All Articles