MONGOENGINE如何获得只知道其中一个字段值的文档



我的应用程序使用flask mongoengine。我有文档-用户。我只需要得到那些名字为";Tim";(例如(。但我不知道写名字的地方到底是什么名字。它可以是——";name"用户名"user_name"等

请求应该是这样的:

users = User.objects.filter(**="Tim")
where ** - field, which i don't know

也许还有其他方法可以做到这一点??

您需要使用MongoEngine(链接(的高级查询功能

from mongoengine import Document, StringField, Q

class Person(Document):
firstname = StringField()
lastname = StringField()
Person(firstname='Brian', lastname = 'Stella').save()
Person(firstname='Stella', lastname = 'Smith').save()
# Following query will found the 2 documents
Person.objects(Q(firstname__contains="Stella") | Q(lastname__contains="Stella")).count()

最新更新