MongoDB - 聚合阶段 $match、$sort、$limit

ops/2024/10/18 5:45:22/

文章目录

    • 1. $match 聚合阶段
      • 1. 构造测试数据
      • 2. $match 示例
      • 3. $match 示例
    • 2. $sort 聚合阶段
      • 1. 排序一致性问题
      • 2. $sort 示例
    • 3. $limit 聚合阶段

1. $match 聚合阶段

$match 接受一个指定查询条件的文档。

$match 阶段语法:

{ $match: { <query> } }

$match 查询语法与读取操作查询语法相同,即 $match 不接受原始聚合表达式。要在 $match 中包含聚合表达式,请使用 $expr 查询表达式:

{ $match: { $expr: { <aggregation expression> } } }

尽可能早地将 $match 放在聚合管道中,由于 $match 限制了聚合管道中的文档总数,因此早期的 $match 操作会最大限度地减少管道中的处理量。

1. 构造测试数据

db.articles.drop()db.articles.insertMany([{"_id": ObjectId("512bc95fe835e68f199c8686"),"author": "dave","score": 80,"views": 100},{"_id": ObjectId("512bc962e835e68f199c8687"),"author": "dave","score": 85,"views": 521},{"_id": ObjectId("55f5a192d4bede9ac365b257"),"author": "ahn","score": 60,"views": 1000},{"_id": ObjectId("55f5a192d4bede9ac365b258"),"author": "li","score": 55,"views": 5000},{"_id": ObjectId("55f5a1d3d4bede9ac365b259"),"author": "annT","score": 60,"views": 50},{"_id": ObjectId("55f5a1d3d4bede9ac365b25a"),"author": "li","score": 94,"views": 999},{"_id": ObjectId("55f5a1d3d4bede9ac365b25b"),"author": "ty","score": 95,"views": 1000}
])

2. $match 示例

使用 $match 来执行简易等值匹配,$match 会选择 author 字段等于 dave 的文档,而聚合返回以下内容:

db.articles.aggregate([ { $match : { author : "dave" } } ]
);
// 1
{"_id": ObjectId("512bc95fe835e68f199c8686"),"author": "dave","score": 80,"views": 100
}// 2
{"_id": ObjectId("512bc962e835e68f199c8687"),"author": "dave","score": 85,"views": 521
}

SpringBoot 整合 MongoDB 实现:

@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void aggregateTest() {// $match阶段Criteria criteria = Criteria.where("author").is("dave");MatchOperation match = Aggregation.match(criteria);Aggregation aggregation = Aggregation.newAggregation(match);// 执行聚合管道操作AggregationResults<Article> results= mongoTemplate.aggregate(aggregation, Article.class, Article.class);List<Article> mappedResults = results.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);//Article(id=512bc95fe835e68f199c8686, author=dave, score=80, views=100)//Article(id=512bc962e835e68f199c8687, author=dave, score=85, views=521)}
}

这里输出文档直接使用了Article.class,可以重新定义实体类接收输出文档的字段:

@Data
public class AggregationResult {@Idprivate String id;private String author;private int score;private int views;
}
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void aggregateTest() {// $match阶段Criteria criteria = Criteria.where("author").is("dave");MatchOperation match = Aggregation.match(criteria);Aggregation aggregation = Aggregation.newAggregation(match);// 执行聚合管道操作AggregationResults<AggregationResult> results= mongoTemplate.aggregate(aggregation, Article.class, AggregationResult.class);List<AggregationResult> mappedResults = results.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);//AggregationResult(id=512bc95fe835e68f199c8686, author=dave, score=80, views=100)//AggregationResult(id=512bc962e835e68f199c8687, author=dave, score=85, views=521)}
}

3. $match 示例

使用 $match 管道操作符选择要处理的文档,然后将结果导入到 $group 管道操作符,以计算文档的数量:

db.articles.aggregate( [// 第一阶段{ $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } },// 第二阶段{ $group: { _id: null, count: { $sum: 1 } } }
] );

第一阶段:

$match 阶段选择 score 大于 70 但小于 90views 大于或等于 1000 的文档。

第二阶段:

m a t c h 阶段筛选的文档通过管道传送到 ‘ match 阶段筛选的文档通过管道传送到 ` match阶段筛选的文档通过管道传送到group` 阶段进行计数。

// 1
{"_id": null,"count": 5
}

SpringBoot 整合 MongoDB 实现:

@Data
@Document(collection = "articles")
public class Article {@Idprivate String id;private String author;private int score;private int views;
}
@Data
public class AggregationResult {private String id;private Integer count;
}
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void aggregateTest() {// 第一阶段Criteria criteria = new Criteria();criteria.orOperator(Criteria.where("score").gt(70).lt(90), Criteria.where("views").gte(1000));MatchOperation match = Aggregation.match(criteria);// 第二阶段GroupOperation group = Aggregation.group().count().as("count");// 组合上面的2个阶段Aggregation aggregation = Aggregation.newAggregation(match,group);// 执行聚合管道操作AggregationResults<AggregationResult> results= mongoTemplate.aggregate(aggregation, Article.class, AggregationResult.class);List<AggregationResult> mappedResults = results.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);// AggregationResult(id=null, count=5)}
}

2. $sort 聚合阶段

$sort 将所有输入文档进行排序,然后按照排序将其返回至管道。

{ $sort: { <field1>: <sort order>, <field2>: <sort order> ... } }

$sort 接受排序依据的字段及相应排序顺序的文档。当 sort order=1时升序排序,sort order=-1 降序排序。

如果对多个字段进行排序,则按从左到右的顺序进行排序。例如,在上面的表单中,文档首先按 field1 排序。然后,具有相同 field1 值的文档将按 field2 进一步排序。

1. 排序一致性问题

MongoDB 不按特定顺序将文档存储在集合中。对包含重复值的字段进行排序时,可能会以任何顺序返回包含这些值的文档。如果需要一致的排序顺序,请在排序中至少纳入一个包含唯一值的字段。

db.restaurants.drop()db.restaurants.insertMany( [{ "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"},{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"},{ "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"},{ "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"},{ "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"},
] )

以下命令使用 $sort 阶段对 borough 字段进行排序:

db.restaurants.aggregate([{ $sort : { borough : 1 } }]
)

在此示例中,排序顺序可能不一致,因为 borough 字段包含 ManhattanBrooklyn 的重复值。文档按 borough 的字母顺序返回,但具有 borough 重复值的文档的顺序在多次执行同一排序中可能不相同。

要实现一致的排序,可以在排序中添加一个仅包含唯一值的字段。以下命令使用 $sort 阶段对 borough 字段和 _id 字段进行排序:

db.restaurants.aggregate([{ $sort : { borough : 1, _id: 1 } }]
)

由于 _id 字段始终保证包含唯一值,因此在同一排序的多次执行中返回的排序顺序将始终相同。

2. $sort 示例

db.articles.drop()db.articles.insertMany([{"_id": ObjectId("512bc95fe835e68f199c8686"),"author": "dave","score": 80,"views": 100},{"_id": ObjectId("512bc962e835e68f199c8687"),"author": "dave","score": 85,"views": 521},{"_id": ObjectId("55f5a192d4bede9ac365b257"),"author": "ahn","score": 60,"views": 1000},{"_id": ObjectId("55f5a192d4bede9ac365b258"),"author": "li","score": 55,"views": 5000},{"_id": ObjectId("55f5a1d3d4bede9ac365b259"),"author": "annT","score": 55,"views": 50},{"_id": ObjectId("55f5a1d3d4bede9ac365b25a"),"author": "li","score": 94,"views": 999},{"_id": ObjectId("55f5a1d3d4bede9ac365b25b"),"author": "ty","score": 95,"views": 1000}
])

对于要作为排序依据的一个或多个字段,可以将排序顺序设置为 1-1 以分别指定升序或降序。

db.articles.aggregate([{ $sort : { score : -1, views: 1 } }]
)
// 1
{"_id": ObjectId("55f5a1d3d4bede9ac365b25b"),"author": "ty","score": 95,"views": 1000
}// 2
{"_id": ObjectId("55f5a1d3d4bede9ac365b25a"),"author": "li","score": 94,"views": 999
}// 3
{"_id": ObjectId("512bc962e835e68f199c8687"),"author": "dave","score": 85,"views": 521
}// 4
{"_id": ObjectId("512bc95fe835e68f199c8686"),"author": "dave","score": 80,"views": 100
}// 5
{"_id": ObjectId("55f5a192d4bede9ac365b257"),"author": "ahn","score": 60,"views": 1000
}// 6
{"_id": ObjectId("55f5a1d3d4bede9ac365b259"),"author": "annT","score": 55,"views": 50
}// 7
{"_id": ObjectId("55f5a192d4bede9ac365b258"),"author": "li","score": 55,"views": 5000
}

SpringBoot 整合 MongoDB:

@Data
@Document(collection = "articles")
public class Article {@Idprivate String id;private String author;private int score;private int views;
}@Data
public class AggregationResult {@Idprivate String id;private String author;private int score;private int views;
}
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void aggregateTest() {// $sort阶段SortOperation sortOperation = Aggregation.sort(Sort.by(Sort.Direction.DESC, "score")).and(Sort.by(Sort.Direction.ASC, "views"));Aggregation aggregation = Aggregation.newAggregation(sortOperation);// 执行聚合查询AggregationResults<AggregationResult> results= mongoTemplate.aggregate(aggregation, Article.class, AggregationResult.class);List<AggregationResult> mappedResults = results.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);//AggregationResult(id=55f5a1d3d4bede9ac365b25b, author=ty, score=95, views=1000)//AggregationResult(id=55f5a1d3d4bede9ac365b25a, author=li, score=94, views=999)//AggregationResult(id=512bc962e835e68f199c8687, author=dave, score=85, views=521)//AggregationResult(id=512bc95fe835e68f199c8686, author=dave, score=80, views=100)//AggregationResult(id=55f5a192d4bede9ac365b257, author=ahn, score=60, views=1000)//AggregationResult(id=55f5a1d3d4bede9ac365b259, author=annT, score=55, views=50)//AggregationResult(id=55f5a192d4bede9ac365b258, author=li, score=55, views=5000)}
}

3. $limit 聚合阶段

$limit 聚合阶段限制传递至管道。$limit 取一个正整数,用于指定传递的最大文档数量:

{ $limit: <positive 64-bit integer> }

如果将 $limit 阶段与以下任何一项一起使用:

  • $sort 聚合阶段
  • sort() 方法
  • sort 命令

在将结果传递到$limit阶段之前,请务必在排序中至少包含一个包含唯一值的字段。

db.articles.drop()db.articles.insertMany([{"_id": ObjectId("512bc95fe835e68f199c8686"),"author": "dave","score": 80,"views": 100},{"_id": ObjectId("512bc962e835e68f199c8687"),"author": "dave","score": 85,"views": 521},{"_id": ObjectId("55f5a192d4bede9ac365b257"),"author": "ahn","score": 60,"views": 1000},{"_id": ObjectId("55f5a192d4bede9ac365b258"),"author": "li","score": 55,"views": 5000},{"_id": ObjectId("55f5a1d3d4bede9ac365b259"),"author": "annT","score": 60,"views": 50},{"_id": ObjectId("55f5a1d3d4bede9ac365b25a"),"author": "li","score": 94,"views": 999},{"_id": ObjectId("55f5a1d3d4bede9ac365b25b"),"author": "ty","score": 95,"views": 1000}
])
db.articles.aggregate([{ $limit : 5 }
]);
// 1
{"_id": ObjectId("512bc95fe835e68f199c8686"),"author": "dave","score": 80,"views": 100
}// 2
{"_id": ObjectId("512bc962e835e68f199c8687"),"author": "dave","score": 85,"views": 521
}// 3
{"_id": ObjectId("55f5a192d4bede9ac365b257"),"author": "ahn","score": 60,"views": 1000
}// 4
{"_id": ObjectId("55f5a192d4bede9ac365b258"),"author": "li","score": 55,"views": 5000
}// 5
{"_id": ObjectId("55f5a1d3d4bede9ac365b259"),"author": "annT","score": 55,"views": 50
}

SpringBoot 整合 MongoDB:

@Data
@Document(collection = "articles")
public class Article {@Idprivate String id;private String author;private int score;private int views;
}@Data
public class AggregationResult {@Idprivate String id;private String author;private int score;private int views;
}
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void aggregateTest() {// $sort阶段LimitOperation limitOperation = Aggregation.limit(5);Aggregation aggregation = Aggregation.newAggregation(limitOperation);// 执行聚合查询AggregationResults<AggregationResult> results= mongoTemplate.aggregate(aggregation, Article.class, AggregationResult.class);List<AggregationResult> mappedResults = results.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);//AggregationResult(id=512bc95fe835e68f199c8686, author=dave, score=80, views=100)//AggregationResult(id=512bc962e835e68f199c8687, author=dave, score=85, views=521)//AggregationResult(id=55f5a192d4bede9ac365b257, author=ahn, score=60, views=1000)//AggregationResult(id=55f5a192d4bede9ac365b258, author=li, score=55, views=5000)//AggregationResult(id=55f5a1d3d4bede9ac365b259, author=annT, score=55, views=50)}
}

http://www.ppmy.cn/ops/84783.html

相关文章

【SOC 芯片设计 DFT 学习专栏 -- DFT DRC规则检查】

请阅读【嵌入式及芯片开发学必备专栏】 请阅读【芯片设计 DFT 学习系列 】 如有侵权&#xff0c;请联系删除 转自&#xff1a; 芯爵ChipLord 2024年07月10日 12:00 浙江 文章目录 概述DRC的概念Tessent DRC检查的概述时钟相关检查扫描相关检查BIST规则检查预DFT时钟规则检查 …

node后端项目使用webpack打包教程,target: “node“

node后端项目使用webpack打包教程 安装webpack webpack-cli pnpm add webpack webpack-cli -Dwebpack 能够为多种环境 或 target构建编辑。想要理解什么是target的详细信息。 taget string [string] false 告知 webpack 为目标(target)指定一个环境。默认值为 browserslist&a…

SEO与数据中心代理IP的结合能带来哪些便利?

本文将探讨将SEO与数据中心代理IP结合所带来的好处&#xff0c;以及如何利用这种组合来提升网站在搜索引擎中的排名和可见性。 1. 数据中心代理IP的作用和优势 数据中心代理IP指的是由数据中心提供的IP地址&#xff0c;用于隐藏真实服务器的位置和身份。与其他类型的代理IP相…

Winform上位机TCP客户端/服务端、串口通信

Winform上位机TCP客户端/服务端、串口通信 背景 日常练习&#xff0c;着急换工作&#xff0c;心态都快乱了。 工具 串口调试助手 网络调试助手 代码 客户端 using Microsoft.VisualBasic.Logging; using System.Net.Sockets; using System.Text;namespace TcpClientDem…

Arraylist与LinkedList的区别

Arraylist 概念 Arraylist非线程安全Arraylist 底层使用的是Object数组ArrayList 采用数组存储&#xff0c;插入和删除元素的时间复杂度受元素位置的影响ArrayList 支持快速随机访问,就是通过元素的序号快速获取元素对象ArrayList的空间浪费主要体现在列表的结尾会预留一定的容…

springSecurity学习之springSecurity web如何取得用户信息

web如何取得用户信息 之前说过SecurityContextHolder默认使用的是ThreadLocal来进行存储的&#xff0c;而且每次都会清除&#xff0c;但是web每次请求都会验证用户权限&#xff0c;这是如何做到的呢&#xff1f; 这是通过SecurityContextPersistenceFilter来实现的&#xff0…

MySQL中,除了使用LIKE进行模糊搜索外,还有其他几种方法可以执行搜索操作

在PHP和MySQL中&#xff0c;除了使用LIKE进行模糊搜索外&#xff0c;还有其他几种方法可以执行搜索操作&#xff0c;具体使用哪种方法取决于你的具体需求&#xff08;如性能、精确度、查询的复杂性等&#xff09;。以下是一些常用的搜索方法&#xff1a; REGEXP 或 RLIKE&…

二阶段测试:

二阶段测试&#xff1a; 架构&#xff1a; 服务器类型部署组件ip地址DR1调度服务器 主&#xff08;ha01&#xff09;KeepalivedLVS-DR192.168.60.30DR2调度服务器 备 (ha02)KeepalivedLVS-DR192.168.60.40web1节点服务器 (slave01)NginxTomcatMySQL 备MHA managerMHA node192.…