Mongodb中数据聚合之聚合管道aggregate
在之前的两篇文章 Mongodb中数据聚合之基本聚合函数count、distinct、group 和 Mongodb中数据聚合之MapReduce 中,我们已经对数据聚合提供了两种实现方式,今天,在这篇文章中,我们讲讲在Mongodb中的另外一种数据聚合实现方式——聚合管道aggregate。 面对
在之前的两篇文章Mongodb中数据聚合之基本聚合函数count、distinct、group >和Mongodb中数据聚合之MapReduce >中,我们已经对数据聚合提供了两种实现方式,今天,在这篇文章中,我们讲讲在Mongodb中的另外一种数据聚合实现方式——聚合管道aggregate。
面对着广大用户对数据统计的需求,Mongodb从2.2版本之后便引入了新的功能聚合框架(aggregation framework),它是数据聚合的新框架,这个概念类似于数据处理中的管道。每个文档通过一个由多个节点组成的管道,每个节点都有自己的特殊的作用(分组、过滤等),文档经过由多个节点组成的管道后最终得到输出结果。管道基本的功能有两种:(1)对文档进行过滤,筛选出符合条件的文档;(2)对文档进行变换,改变文档的输出结构。
聚合管道的使用方式:db.collection.aggregate();
对于管道中的多个节点可以使用以下几种管道操作符,下面对各种管道操作符的功能进行描述:
$project:修改文档的结构(重命名、增加或删除域),也可以用于创建计算结果以及嵌套文档。
$match:过滤数据,只输出符合条件的文档。
$limit:限制MongoDB聚合管道返回的文档数。
$skip:在聚合管道中跳过指定数量的文档。
$unwind:将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值。
$group:将集合中的文档分组,可用于统计结果。
$sort:文档排序输出。
$geoNear:输出接近某一地理位置的有序文档。
举两个简单的例子:
db.article.aggregate( { $project : { title : 1 , author : 1 , }} );
db.articles.aggregate( [ { $match : { score : { $gt : 70, $lte : 90 } } }, { $group: { _id: user, count: { $sum: 1 } } } ] );
下面对聚合管道使用过程中需要注意的地方进行说明:
(1)管道的是具有先后顺序的。
(2)$group操作目前是在内存中处理的,因此,不能对大量的文档进行使用该种方式进行分组操作;
(3)使用$unwind对数组中的字段值进行拆分时需要注意不能忘记写$符号,如{$unwind:"$tags"},tags字段前面有个$号;
(4)MongoDB 24.对内存做了优化,如果$sort出现在$limit之前,$sort只会对前$limit个文档进行操作,在内存中也只会保留前$limit个文档,节省了内存
(5)$sort操作是在内存中进行的,如果其占有的内存超过物理内存的10%,程序会产生错误
(6)管道的输出结果大小不能大于16M,超过会出现错误。
(7)如果一个管道操作符在执行过程中所占用的内存超过系统内存容量的10%,则会报错;
(8)聚合管道可以提供很好的性能和一致的接口,使用起来比较简单,对于一些简单的固定的聚集操作可以使用管道,但是对于一些复杂的、大量数据集的聚合任务还是使用MapReduce。
至此,关于Mongodb数据库内的数据聚合操作的简单描述便结束了,如果大家想更深入的学习了解,我觉得官网才是最好的教材,

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

GitLab Database Deployment Guide on CentOS System Selecting the right database is a key step in successfully deploying GitLab. GitLab is compatible with a variety of databases, including MySQL, PostgreSQL, and MongoDB. This article will explain in detail how to select and configure these databases. Database selection recommendation MySQL: a widely used relational database management system (RDBMS), with stable performance and suitable for most GitLab deployment scenarios. PostgreSQL: Powerful open source RDBMS, supports complex queries and advanced features, suitable for handling large data sets. MongoDB: Popular NoSQL database, good at handling sea

Encrypting MongoDB database on a Debian system requires following the following steps: Step 1: Install MongoDB First, make sure your Debian system has MongoDB installed. If not, please refer to the official MongoDB document for installation: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/Step 2: Generate the encryption key file Create a file containing the encryption key and set the correct permissions: ddif=/dev/urandomof=/etc/mongodb-keyfilebs=512

To set up a MongoDB user, follow these steps: 1. Connect to the server and create an administrator user. 2. Create a database to grant users access. 3. Use the createUser command to create a user and specify their role and database access rights. 4. Use the getUsers command to check the created user. 5. Optionally set other permissions or grant users permissions to a specific collection.
