文档菜单

文档首页开发应用程序Python 驱动程序PyMongo

指定查询

本页内容

  • 概述
  • 示例数据
  • 精确匹配
  • 比较运算符
  • 逻辑运算符
  • 数组运算符
  • 元素运算符
  • 评估运算符
  • 其他信息
  • API 文档

在本指南中,您可以学习如何使用 PyMongo 指定查询。

您可以通过创建一个 查询过滤器 来细化查询返回的文档集。查询过滤器是一个表达式,它指定 MongoDB 用于在读取或写入操作中匹配文档的搜索标准。在查询过滤器中,您可以提示驱动程序搜索与查询完全匹配的文档,或者您可以将查询过滤器组合起来以表达更复杂的匹配标准。

本指南中的示例在名为fruits 的集合上运行操作,该集合包含以下文档

{ "_id": 1, "name": "apples", "qty": 5, "rating": 3, "color": "red", "type": ["fuji", "honeycrisp"] },
{ "_id": 2, "name": "bananas", "qty": 7, "rating": 4, "color": "yellow", "type": ["cavendish"] },
{ "_id": 3, "name": "oranges", "qty": 6, "rating": 2, "type": ["naval", "mandarin"] },
{ "_id": 4, "name": "pineapple", "qty": 3, "rating": 5, "color": "yellow" },

以下代码示例展示了如何创建数据库和集合,然后向集合中插入示例文档

from pymongo import MongoClient
uri = "<connection string URI>"
client = MongoClient(uri)
try:
database = client["sample_fruit"]
collection = database["fruits"]
collection.insert_many([
{ "_id": 1, "name": "apples", "qty": 5, "rating": 3, "color": "red", "type": ["fuji", "honeycrisp"] },
{ "_id": 2, "name": "bananas", "qty": 7, "rating": 4, "color": "yellow", "type": ["cavendish"] },
{ "_id": 3, "name": "oranges", "qty": 6, "rating": 2, "type": ["naval", "mandarin"] },
{ "_id": 4, "name": "pineapple", "qty": 3, "rating": 5, "color": "yellow" },
])
client.close()
except Exception as e:
raise Exception("Error inserting documents: ", e)

字面值查询返回与查询过滤器完全匹配的文档。

以下示例将查询过滤器指定为 find() 方法的参数。该代码返回所有具有 color 字段值为 "yellow" 的文档

results = collection.find({ "color": "yellow" })
{'_id': 2, 'name': 'bananas', 'qty': 7, 'rating': 4, 'color': 'yellow', 'type': ['cavendish']}
{'_id': 4, 'name': 'pineapple', 'qty': 3, 'rating': 5, 'color': 'yellow'}

提示

查找所有文档

要查找集合中的所有文档,请调用 find() 方法并传递一个空查询过滤器。以下示例查找集合中的所有文档

results = collection.find({})

比较运算符通过将文档字段值与查询过滤器中指定的值进行比较来评估。以下是一些常见的比较运算符

  • $gt:大于

  • $lte:小于或等于

  • $ne:不等于

要查看比较运算符的完整列表,请参阅 MongoDB 服务器手册中的比较查询运算符指南。

以下示例将比较运算符指定为查询过滤器的参数,传递给 find() 方法。该代码返回所有具有 rating 字段值大于 2 的文档

results = collection.find({ "rating": { "$gt" : 2 }})
for f in results:
print(f)
{'_id': 1, 'name': 'apples', 'qty': 5, 'rating': 3, 'color': 'red', 'type': ['fuji', 'honeycrisp']}
{'_id': 2, 'name': 'bananas', 'qty': 7, 'rating': 4, 'color': 'yellow', 'type': ['cavendish']}
{'_id': 4, 'name': 'pineapple', 'qty': 3, 'rating': 5, 'color': 'yellow'}

逻辑运算符通过应用于两个或多个表达式结果集的逻辑来匹配文档。以下是一些逻辑运算符

  • $and,它返回所有符合所有子句条件的文档

  • $or,它返回所有符合一个子句条件的文档

  • $nor,它返回所有不符合任何子句条件的文档

  • $not,它返回所有不符合表达式的文档

要了解有关逻辑运算符的更多信息,请参阅 MongoDB 服务器手册中的逻辑查询运算符指南。

以下示例将逻辑运算符指定为查询过滤器的参数,传递给 find() 方法。该代码返回所有具有 qty 字段值大于 5color 字段值为 "yellow" 的文档

results = collection.find({
"$or": [
{ "qty": { "$gt": 5 }},
{ "color": "yellow" }
]
})
for f in results:
print(f)
{'_id': 2, 'name': 'bananas', 'qty': 7, 'rating': 4, 'color': 'yellow', 'type': ['cavendish']}
{'_id': 3, 'name': 'oranges', 'qty': 6, 'rating': 2, 'type': ['naval', 'mandarin']}
{'_id': 4, 'name': 'pineapple', 'qty': 3, 'rating': 5, 'color': 'yellow'}

数组运算符根据数组字段中的值或元素数量匹配文档。以下是一些可用的数组运算符

  • $all,它返回包含查询中所有元素的数组的文档

  • $elemMatch,它返回数组字段中的元素符合查询中所有条件的文档

  • $size,它返回具有指定大小的数组的所有文档

要了解有关数组运算符的更多信息,请参阅MongoDB服务器手册中的数组查询运算符指南。

以下示例在查询过滤器中将数组运算符指定为find()方法的参数。该代码返回所有具有包含2个元素的type数组字段的文档。

results = collection.find({
"type" : { "$size": 2 }
})
for f in results:
print(f)
{'_id': 1, 'name': 'apples', 'qty': 5, 'rating': 3, 'color': 'red', 'type': ['fuji', 'honeycrisp']}
{'_id': 3, 'name': 'oranges', 'qty': 6, 'rating': 2, 'type': ['naval', 'mandarin']}

元素运算符根据字段的存在或类型查询数据。

要了解有关元素运算符的更多信息,请参阅MongoDB服务器手册中的元素查询运算符指南。

以下示例在查询过滤器中将元素运算符指定为find()方法的参数。该代码返回所有具有color字段的文档。

results = collection.find( { "color" : { "$exists": "true" }} )
for f in results:
print(f)
{'_id': 1, 'name': 'apples', 'qty': 5, 'rating': 3, 'color': 'red', 'type': ['fuji', 'honeycrisp']}
{'_id': 2, 'name': 'bananas', 'qty': 7, 'rating': 4, 'color': 'yellow', 'type': ['cavendish']}
{'_id': 4, 'name': 'pineapple', 'qty': 3, 'rating': 5, 'color': 'yellow'}

评估运算符根据对单个字段或整个集合文档的评估返回数据。

以下是常见评估运算符的列表

  • $text,在文档上执行文本搜索

  • $regex,返回与指定的正则表达式匹配的文档

  • $mod,对一个字段的值执行模运算,并返回余数为指定值的文档

要查看评估运算符的完整列表,请参阅MongoDB服务器手册中的评估查询运算符指南。

以下示例在查询过滤器中将评估运算符指定为find()方法的参数。该代码使用正则表达式返回所有具有至少两个连续的"p"字符的name字段值的文档。

results = collection.find({ "name" : { "$regex" : "p{2,}" }} )
for f in results:
print(f)
{'_id': 1, 'name': 'apples', 'qty': 5, 'rating': 3, 'color': 'red', 'type': ['fuji', 'honeycrisp']}
{'_id': 4, 'name': 'pineapple', 'qty': 3, 'rating': 5, 'color': 'yellow'}

要了解有关查询文档的更多信息,请参阅MongoDB服务器手册中的查询文档指南。

要了解有关使用PyMongo检索文档的更多信息,请参阅检索数据。

要了解本指南中讨论的任何方法或类型,请参阅以下API文档

← 从 MongoDB 读取数据