文档菜单
文档首页
/ / /
Node.js 驱动
/

筛选子集

本页面内容

  • 简介
  • 聚合任务摘要
  • 开始之前
  • 教程
  • 为工程师添加一个匹配阶段
  • 添加一个排序阶段,从最年轻到最老排序
  • 添加一个限制阶段,只显示三个结果
  • 添加一个未设置阶段,移除不必要的字段
  • 运行聚合管道
  • 解释结果

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

  • 通过字段值匹配文档的子集

  • 格式化结果文档

提示

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

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

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

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

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

const personColl = 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

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

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

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

返回

聚合教程