Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a item from a list(ListField) by id in MongoEngine?

structure:

{title: 'test', comments: [{id:1, title: ''}, {id: 8, title: ''}]}

i need remove the id=8 item, thanks.

like image 884
Jerry Avatar asked Oct 21 '11 07:10

Jerry


2 Answers

Hi you can pull items from an array:

https://github.com/hmarr/mongoengine/blob/master/mongoengine/queryset.py

See $pull: http://www.mongodb.org/display/DOCS/Updating#Updating-%24pull

like image 162
Ross Avatar answered Oct 20 '22 14:10

Ross


You need to use $pull operator here :

http://www.mongodb.org/display/DOCS/Updating#Updating-%24pull

db.collection.update({'title':'test'},{$pull : { 'comments' : { 'id' : 8 }});
like image 2
DhruvPathak Avatar answered Oct 20 '22 13:10

DhruvPathak