文档菜单

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

筛选子集

本页内容

  • 简介
  • 聚合任务摘要
  • 开始之前
  • 教程
  • 为工程师添加匹配阶段
  • 添加排序阶段以按年龄从小到大排序
  • 添加限制阶段以仅显示三个结果
  • 添加未设置阶段以删除不需要的字段
  • 运行聚合管道
  • 解释结果

在这个教程中,您可以通过完成并运行一个示例应用程序,学习如何使用 PyMongo 构建聚合管道,在集合上执行聚合,并打印结果。此聚合执行以下操作

  • 根据字段值匹配文档的子集

  • 格式化结果文档

本教程演示了如何查询集合以获取集合中的特定文档子集。结果包含描述三位最年轻工程师的文档。

此示例使用一个集合,persons,其中包含描述人物的文档。每个文档包括一个人的姓名、出生日期、职业和其他详细信息。

在开始本教程之前,请完成聚合模板应用程序说明,以设置一个可工作的Python应用程序。

设置应用程序后,通过在应用程序中添加以下代码来访问persons集合

person_coll = agg_db["persons"]

删除集合中的任何现有数据,并按以下代码将示例数据插入到persons集合中

person_coll.delete_many({})
person_data = [
{
"person_id": "6392529400",
"firstname": "Elise",
"lastname": "Smith",
"dateofbirth": datetime(1972, 1, 13, 9, 32, 7),
"vocation": "ENGINEER",
"address": {
"number": 5625,
"street": "Tipa Circle",
"city": "Wojzinmoj",
}
},
{
"person_id": "1723338115",
"firstname": "Olive",
"lastname": "Ranieri",
"dateofbirth": datetime(1985, 5, 12, 23, 14, 30),
"gender": "FEMALE",
"vocation": "ENGINEER",
"address": {
"number": 9303,
"street": "Mele Circle",
"city": "Tobihbo",
}
},
{
"person_id": "8732762874",
"firstname": "Toni",
"lastname": "Jones",
"dateofbirth": datetime(1991, 11, 23, 16, 53, 56),
"vocation": "POLITICIAN",
"address": {
"number": 1,
"street": "High Street",
"city": "Upper Abbeywoodington",
}
},
{
"person_id": "7363629563",
"firstname": "Bert",
"lastname": "Gooding",
"dateofbirth": datetime(1941, 4, 7, 22, 11, 52),
"vocation": "FLORIST",
"address": {
"number": 13,
"street": "Upper Bold Road",
"city": "Redringtonville",
}
},
{
"person_id": "1029648329",
"firstname": "Sophie",
"lastname": "Celements",
"dateofbirth": datetime(1959, 7, 6, 17, 35, 45),
"vocation": "ENGINEER",
"address": {
"number": 5,
"street": "Innings Close",
"city": "Basilbridge",
}
},
{
"person_id": "7363626383",
"firstname": "Carl",
"lastname": "Simmons",
"dateofbirth": datetime(1998, 12, 26, 13, 13, 55),
"vocation": "ENGINEER",
"address": {
"number": 187,
"street": "Hillside Road",
"city": "Kenningford",
}
}
]
person_coll.insert_many(person_data)
1

首先,添加一个$match阶段,以找到vocation字段的值为"ENGINEER"的文档

pipeline.append({
"$match": {
"vocation": "ENGINEER"
}
})
2

接下来,添加一个$sort阶段,按dateofbirth字段的降序排序文档,以便首先列出年轻人。由于Python字典不保持其元素的顺序,因此请使用SONOrderedDict对象代替

pipeline.append({
"$sort": {
"dateofbirth": -1
}
})
3

接下来,向管道中添加一个$limit阶段,以仅输出结果中的前三个文档。

pipeline.append({
"$limit": 3
})
4

最后,添加一个$unset阶段。该$unset阶段从结果文档中删除不必要的字段

pipeline.append({
"$unset": [
"_id",
"address"
]
})

提示

使用$unset运算符而不是$project,以避免在将具有不同字段的文档添加到集合时修改聚合管道。

5

将以下代码添加到应用程序末尾以对persons集合执行聚合

aggregation_result = person_coll.aggregate(pipeline)

最后,在您的shell中运行以下命令以启动您的应用程序

python3 agg_tutorial.py
6

聚合结果包含三个文档。这些文档代表拥有“工程师”职业的三位最年轻的人,按年龄从年轻到老排序。结果省略了_id地址字段。

{
'person_id': '7363626383',
'firstname': 'Carl',
'lastname': 'Simmons',
'dateofbirth': datetime.datetime(1998, 12, 26, 13, 13, 55),
'vocation': 'ENGINEER'
}
{
'person_id': '1723338115',
'firstname': 'Olive',
'lastname': 'Ranieri',
'dateofbirth': datetime.datetime(1985, 5, 12, 23, 14, 30),
'gender': 'FEMALE',
'vocation': 'ENGINEER'
}
{
'person_id': '6392529400',
'firstname': 'Elise',
'lastname': 'Smith',
'dateofbirth': datetime.datetime(1972, 1, 13, 9, 32, 7),
'vocation': 'ENGINEER'
}

要查看本教程的完整代码,请参阅GitHub上的完成的过滤子集应用程序

← 聚合教程