Mongoengine 简明教程
MongoEngine - QuerySet Methods
QuerySet 对象拥有以下用于查询数据库的有用方法。
The QuerySet object possesses following useful methods for querying the database.
first()
将返回满足查询的第一个文档。以下代码将返回 products 集合中的第一个文档,价格 < 20000。
First document satisfying the query is returned. Following code will return first document in products collection, that has price < 20000.
qset=products.objects(price__lt=20000)
doc=qset.first()
print ('Name:',doc.Name, 'Price:',doc.price)
exclude()
这将导致从查询集中排除提到的字段。此处,Document 类的 to_json() 方法用于获取 Document 的 JSON 化版本。ProductID 字段不会出现在结果中。
This will cause mentioned fields to be excluded from Query Set. Here, to_json() mehod of Document class is used to obtain JSONified version of Document. ProductID field will not appear in the result.
for product in products.objects.exclude('ProductID'):
print (product.to_json())
Output
{"_id": {"$oid": "5c8dec275405c12e3402423c"}, "Name": "Laptop", "price": 25000}
{"_id": {"$oid": "5c8dec275405c12e3402423d"}, "Name": "TV", "price": 50000}
{"_id": {"$oid": "5c8dec275405c12e3402423e"}, "Name": "Router", "price": 2000}
{"_id": {"$oid": "5c8dec275405c12e3402423f"}, "Name": "Scanner", "price": 5000}
{"_id": {"$oid": "5c8dec275405c12e34024240"}, "Name": "Printer", "price": 12500}
fields()
使用此方法控制加载到查询集中的字段。使用字段名作为关键字参数,并设置为 1 以包含,设置为 0 以排除。
Use this method to manipulate which fields to load in the query set. Use field names as keyword arguments and set to 1 to include, 0 to exclude.
for product in products.objects.fields(ProductID=1,price=1):
print (product.to_json())
Output
{"_id": {"$oid": "5c8dec275405c12e3402423c"}, "ProductID": 1, "price": 25000}
{"_id": {"$oid": "5c8dec275405c12e3402423d"}, "ProductID": 2, "price": 50000}
{"_id": {"$oid": "5c8dec275405c12e3402423e"}, "ProductID": 3, "price": 2000}
{"_id": {"$oid": "5c8dec275405c12e3402423f"}, "ProductID": 4, "price": 5000}
{"_id": {"$oid": "5c8dec275405c12e34024240"}, "ProductID": 5, "price": 12500}
在 fields()
方法中将字段关键字参数设置为 0 的效果类似于 exclude()
方法。
Setting field keyword argument to 0 in fields() method works similar to exclude() method.
for product in products.objects.fields(price=0):
print (product.to_json())
Output
{"_id": {"$oid": "5c8dec275405c12e3402423c"}, "ProductID": 1, "Name": "Laptop"}
{"_id": {"$oid": "5c8dec275405c12e3402423d"}, "ProductID": 2, "Name": "TV"}
{"_id": {"$oid": "5c8dec275405c12e3402423e"}, "ProductID": 3, "Name": "Router"}
{"_id": {"$oid": "5c8dec275405c12e3402423f"}, "ProductID": 4, "Name": "Scanner"}
{"_id": {"$oid": "5c8dec275405c12e34024240"}, "ProductID": 5, "Name": "Printer"}
only()
这种方法的效果类似于 fields()
方法。只有与关键字参数对应的字段才会出现在查询集中。
Effect of this method is similar to fields() method. Fields corresponding to keyword arguments only will appear in the query set.
for product in products.objects.only('Name'):
print (product.to_json())
Output
{"_id": {"$oid": "5c8dec275405c12e3402423c"}, "Name": "Laptop"}
{"_id": {"$oid": "5c8dec275405c12e3402423d"}, "Name": "TV"}
{"_id": {"$oid": "5c8dec275405c12e3402423e"}, "Name": "Router"}
{"_id": {"$oid": "5c8dec275405c12e3402423f"}, "Name": "Scanner"}
{"_id": {"$oid": "5c8dec275405c12e34024240"}, "Name": "Printer"}