Mongoengine 简明教程

MongoEngine - Filters

objects 属性是一个 QuerySet 管理器。访问时,它会创建并返回一个 QuerySet。借助于字段名称作为关键字参数,可以对查询进行过滤。例如,从上面的 products 集合中,若要打印产品名为“TV”的文档的详细信息,我们使用 Name 作为关键字参数。

The objects attribute is a QuerySet manager. It creates and returns a QuerySet when accessed. A query can be subjected to filter with the help of field names as keyword arguments. For example, from above products collection, to print details of document with name of product as ‘TV’, we use Name as keyword argument.

for product in products.objects(Name='TV'):
print ('ID:',product.ProductID, 'Name:',product.Name, 'Price:',product.price)

可以将 QuerySet 对象的 filter 方法用于将过滤器应用于查询。以下代码片段也返回了名为“TV”的产品详细信息。

You can use filter method of QuerySet object to apply filter to query. Following code snippet also returns product details with name=’TV’.

qset=products.objects
for product in qset.filter(Name='TV'):
   print ('ID:',product.ProductID, 'Name:',product.Name, 'Price:',product.price)