Sid*_*rth 2 mongodb mongoengine
我有一个ListField(DictField)包含像 -
{'user_id': '12345', 'timestamp' : 'datetime-object'}
Run Code Online (Sandbox Code Playgroud)
在mongoengine中,如何从user_id上查询的List中删除元素.例如,我想删除具有特定user_id的条目.我试过以下 -
update_one(pull__notes__user_id = '12345')
Run Code Online (Sandbox Code Playgroud)
这notes是集合的名称.
此语句返回1但不会从List中删除该元素.我怎样才能做到这一点?
有两种方法:
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'})
Run Code Online (Sandbox Code Playgroud)
B)匹配元素的一部分.使用位置运算符匹配元素并取消设置.
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2469 次 |
| 最近记录: |