Docs 菜单
Docs 主页
/ / /
pymongo
/ /

过滤的子集

在此页面上

  • 简介
  • 聚合任务摘要
  • 开始之前
  • 教程
  • 为工程人员增加一个匹配阶段
  • 添加排序阶段以从最年轻到最年长进行排序
  • 添加限制阶段以仅查看三个结果
  • 添加未设置阶段以删除不需要的字段
  • 运行聚合管道
  • 解释结果

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

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

  • 格式结果文档

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

此示例使用一个collection 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 字典不维护其元素的顺序,因此请改用SON``or ``OrderedDict对象:

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

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

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

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

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

提示

如果将具有不同字段的文档添加到集合中,请使用 $unset 操作符而不是 $project 来避免修改聚合管道。

5

将以下代码添加到应用程序末尾,以对personscollection执行聚合:

aggregation_result = person_coll.aggregate(pipeline)

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

python3 agg_tutorial.py
6

聚合结果包含三个文档。这些文档代表了职业为"ENGINEER"的三个最年轻的人,按从幼到长的顺序排列。结果省略了 _idaddress 字段。

{
'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'
}

要查看本教程的完整代码,请参阅 Completed Filtered Subset App 在Github 上。

后退

聚合教程

来年

群组和总计