Docs 菜单
Docs 主页
/ / /
Node.js 驱动程序
/

筛选子集

在此页面上

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

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

  • 按字段值匹配文档子集

  • 设置结果文档的格式

提示

您还可以使用 Query API 查询集合中的文档子集。要了解如何指定查询,请参阅读取操作指南。

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

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

在开始本教程之前,请完成聚合模板应用说明,设置有效的 Node.js 应用程序。

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

const personColl = await aggDB.collection("persons");

删除集合中的所有现有数据,并将样本数据插入persons集合,如以下代码所示:

await personColl.deleteMany({});
const personData = [
{
person_id: "6392529400",
firstname: "Elise",
lastname: "Smith",
dateofbirth: new Date("1972-01-13T09:32:07Z"),
vocation: "ENGINEER",
address: {
number: 5625,
street: "Tipa Circle",
city: "Wojzinmoj",
},
},
{
person_id: "1723338115",
firstname: "Olive",
lastname: "Ranieri",
dateofbirth: new Date("1985-05-12T23:14:30Z"),
gender: "FEMALE",
vocation: "ENGINEER",
address: {
number: 9303,
street: "Mele Circle",
city: "Tobihbo",
},
},
{
person_id: "8732762874",
firstname: "Toni",
lastname: "Jones",
dateofbirth: new Date("1991-11-23T16:53:56Z"),
vocation: "POLITICIAN",
address: {
number: 1,
street: "High Street",
city: "Upper Abbeywoodington",
},
},
{
person_id: "7363629563",
firstname: "Bert",
lastname: "Gooding",
dateofbirth: new Date("1941-04-07T22:11:52Z"),
vocation: "FLORIST",
address: {
number: 13,
street: "Upper Bold Road",
city: "Redringtonville",
},
},
{
person_id: "1029648329",
firstname: "Sophie",
lastname: "Celements",
dateofbirth: new Date("1959-07-06T17:35:45Z"),
vocation: "ENGINEER",
address: {
number: 5,
street: "Innings Close",
city: "Basilbridge",
},
},
{
person_id: "7363626383",
firstname: "Carl",
lastname: "Simmons",
dateofbirth: new Date("1998-12-26T13:13:55Z"),
vocation: "ENGINEER",
address: {
number: 187,
street: "Hillside Road",
city: "Kenningford",
},
},
];
await personColl.insertMany(personData);
1

首先,添加一个 $match阶段,用于查找vocation字段的值为"ENGINEER"的文档:

pipeline.push({
$match: {
"vocation": "ENGINEER"
},
});
2

接下来,添加一个$sort阶段,根据dateofbirth字段按降序对文档进行排序,从而首先列出最年轻的人:

pipeline.push({
$sort: {
"dateofbirth": -1,
}
});
3

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

pipeline.push({
$limit: 3
});
4

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

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

提示

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

5

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

const aggregationResult = await personColl.aggregate(pipeline);

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

node agg_tutorial.js
6

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

{
person_id: '7363626383',
firstname: 'Carl',
lastname: 'Simmons',
dateofbirth: 1998-12-26T13:13:55.000Z,
vocation: 'ENGINEER'
}
{
person_id: '1723338115',
firstname: 'Olive',
lastname: 'Ranieri',
dateofbirth: 1985-05-12T23:14:30.000Z,
gender: 'FEMALE',
vocation: 'ENGINEER'
}
{
person_id: '6392529400',
firstname: 'Elise',
lastname: 'Smith',
dateofbirth: 1972-01-13T09:32:07.000Z,
vocation: 'ENGINEER'
}

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

← 聚合教程