[英]Pymongo - Exclude a field from results in a raw query
According to MongoEngine Docs on Raw Queries , a raw query can be used in Python when getting a collection of objects like so: 根据“原始查询”上的MongoEngine Docs的说明 ,在获取对象集合时,可以在Python中使用原始查询 ,如下所示:
Page.objects(__raw__={'tags': 'coding'})
What would be an example of using this method to exclude a particular field from the results? 使用此方法从结果中排除特定字段的示例将是什么?
You can't use __raw__
for this, you have 2 options: 您不能为此使用
__raw__
,您有两种选择:
Using mongoengine, you need to use exclude : 使用mongoengine,您需要使用exclude :
Page.objects(__raw__={'tags': 'coding'}).exclude('field1', 'field2')
Using pymongo (the underlying driver) - but then you won't be able to iterate over mongoengine Page
instances but only over simple dict's (ie pymongo's output): 使用pymongo(底层驱动程序)-但是您将不能仅在简单dict(即pymongo的输出)上遍历mongoengine
Page
实例:
page_collection = Page._get_collection()
query = page_collection.find({'tags': 'coding'}, {'field1': False, 'field2': False})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.