3

私は次のListField(DictField)ようなアイテムを含むを持っています-

{'user_id': '12345', 'timestamp' : 'datetime-object'}

mongoengineで、user_idで照会されたリストから要素を削除するにはどうすればよいですか。たとえば、特定のuser_idを持つエントリを削除したいと思います。私は次のことを試しました-

update_one(pull__notes__user_id = '12345')

これnotesがコレクションの名前です。

このステートメントは戻ります1が、リストから要素を削除しません。どうやってやるの?

4

1 に答える 1

8

これを行う2つの方法:

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)要素の一部を一致させます。位置演算子を使用して要素を一致させ、設定を解除します。

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)
于 2012-01-31T13:20:55.763 に答える