我有一个ListField(DictField),里面有像这样的东西-
{'user_id': '12345', 'timestamp' : 'datetime-object'}在mongoengine中,如何从user_id查询的列表中移除元素?例如,我想删除具有特定user_id的条目。我试过以下几种方法-
update_one(pull__notes__user_id = '12345')这里的notes是集合的名称。
此语句返回1,但不从列表中删除元素。我该怎么做呢?
发布于 2012-01-31 21:20:56
执行此操作的两种方法:
A)精确匹配元素:
class Simple(Document):
x = ListField()
Simple.drop_collection()
Simple(x=[{'hello': 'world'}, {'mongo': 'db'}]).save()
// Pull the dict
Simple.objects.update_one(pull__x={'mongo': 'db'})B)匹配元素的一部分。使用positional operator匹配元素并取消其设置。
class Simple(Document):
x = ListField()
Simple.drop_collection()
Simple(x=[{'hello': 'world'}, {'mongo': 'db'}]).save()
// Set to None
Simple.objects.update_one(unset__x__S__mongo='db')
// Pull None
Simple.objects.update_one(pull__x=None)https://stackoverflow.com/questions/9079865
复制相似问题