Home Database Mysql Tutorial MongoDB:聚集管道

MongoDB:聚集管道

Jun 07, 2016 pm 03:59 PM
mongodb pipeline

在MongoDB2.2新出现的。 聚集管道式基于数据处理管道概念建模的数据聚集框架。文档进入一个多阶段能将该文档转化为聚集结果的管道。 聚集管道提供了map-reduce方法了替代物,并在很多聚集任务中是首选的方案,因为map-reduce的复杂性可能是你不希望看到的。

在MongoDB2.2新出现的。

聚集管道式基于数据处理管道概念建模的数据聚集框架。文档进入一个多阶段能将该文档转化为聚集结果的管道。

聚集管道提供了map-reduce方法了替代物,并在很多聚集任务中是首选的方案,因为map-reduce的复杂性可能是你不希望看到的。

\

上图是一个带注释的聚集管道的操作,有两个阶段:$match和$group

聚集管道在值的类型和结果大小上有很多限制。下面简单介绍,

聚集操作在使用aggregate命令时有的限制:

类型限制

聚集管道不是在下列类型的值上进行操作:Symbol,Minkey,MaxKey,DBRef,Code和CodeWSrope

(在MongoDB2.4版本解除了对Binary类型的限制。在MongoDB2.2,管道不能对Binary类型数据操作)

结果大小限制

如果aggregate命令返回的单个文档保护完整的结果集,则该命令在结果集超过BSON Document Size限制时会产生一个错误,目前的大小是16M。为了管理超过这一限制的结果集,aggregate命令当命令返回一个游标(cursor)或把结果保存在一个collection里时,能够返回任意大小的结果集。

(在MongoDB2.6,aggregate命令返回一个游标或把结果存在一个collection时,能不受这个大小限制。db.collection.aggregate()返回一个游标,能返回任意大小的结果集。)

内存限制

在MongoDB2.6有了变化。

管道阶段在RAM有100M的限制。如果超过这一限制,MongoDB会出错。为了允许操作大型数据,可以使用allowDiskUse选项来时聚集管道阶段能往临时文件写数据。

管道

管道,顾名思义就是来自集合的文档通过一个聚集管道的旅行,当通过其中时该管道能转化这些对象。对熟悉Unix shells命令的(如 bash),这个概念和管道(pipe)很类似。

MongoDB的聚集管道以一个集合的文档开始,流动文档从一个管道操作(pipeline operator)到下一个来处理文档。在管道的每一个操作符在文档经过管道时都会转化文档。管道操作符不需要为每一个输入文档产生一个输出文档。操作符可以产生新文档也能过滤文档。管道操作能在一个管道里面重复。

管道表达式

每一个管道操作符接受一个管道表达式作为操作数。管道表达式指出应用在输入文档的转化过程。表达式有一个文档(document)结构,并包含字段,值和操作符。

管道表达式只能操作在管道中的当前文档,不能引用在其他文档的数据:表达式提供了内存(in-memory)文档转化。

一般的,表达式是无状态的,只在聚集过程时有一个例外:Accumulation expressions。累加表达式,使用$group管道,维持他们的状态(如,totals,maximums,mininums和相关数据)作为通过管道的文档过程。

聚集管道行为

在MongoDB,aggregate命令操作单个集合,并在逻辑上传递整个文档给聚集管道。为了优化这个操作,在可能的情况下,应该使用下面策略来避免扫描整个集合。

管道操作符和索引

$match和$sort管道操作符能够利用索引的优势,如果他们在管道的开始位置出现。

(在Mongo2.4新出现的:$geoNear管道操作符能利用地理索引的优势。当使用$geoNear,$geoNear必须在聚集管道的第一阶段出现。)

即使管道使用了索引,聚合操作依然要访问实际的文档。比如,索引不能完全覆盖聚集管道。

(在Mongo2.6之前的版本,对规模非常小的选择情况,索引能够覆盖管道)

提前过滤

如果你的聚集应用仅需要一个集合的一个数据子集,使用 $match,$limit,$skip阶段在文档进入管道时去限制文档.当被放置在管道的开始,$match操作符使用合适的索引去扫描集合匹配的文档.

在管道的开始紧跟着$sort阶段放置$match在逻辑上等价于一个使用排序的单一查询,并可以使用索引.如果可能的话,把$match放置在管道的开头.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Which version is generally used for mongodb? Which version is generally used for mongodb? Apr 07, 2024 pm 05:48 PM

It is recommended to use the latest version of MongoDB (currently 5.0) as it provides the latest features and improvements. When selecting a version, you need to consider functional requirements, compatibility, stability, and community support. For example, the latest version has features such as transactions and aggregation pipeline optimization. Make sure the version is compatible with the application. For production environments, choose the long-term support version. The latest version has more active community support.

The difference between nodejs and vuejs The difference between nodejs and vuejs Apr 21, 2024 am 04:17 AM

Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

How to use pipes to read and write files in Golang? How to use pipes to read and write files in Golang? Jun 04, 2024 am 10:22 AM

File reading and writing through pipes: Create a pipe to read data from the file and pass it through the pipe Receive the data from the pipe and process it Write the processed data to the file Use goroutines to perform these operations concurrently to improve performance

Where is the database created by mongodb? Where is the database created by mongodb? Apr 07, 2024 pm 05:39 PM

The data of the MongoDB database is stored in the specified data directory, which can be located in the local file system, network file system or cloud storage. The specific location is as follows: Local file system: The default path is Linux/macOS:/data/db, Windows: C:\data\db. Network file system: The path depends on the file system. Cloud Storage: The path is determined by the cloud storage provider.

What are the advantages of mongodb database What are the advantages of mongodb database Apr 07, 2024 pm 05:21 PM

The MongoDB database is known for its flexibility, scalability, and high performance. Its advantages include: a document data model that allows data to be stored in a flexible and unstructured way. Horizontal scalability to multiple servers via sharding. Query flexibility, supporting complex queries and aggregation operations. Data replication and fault tolerance ensure data redundancy and high availability. JSON support for easy integration with front-end applications. High performance for fast response even when processing large amounts of data. Open source, customizable and free to use.

What does mongodb mean? What does mongodb mean? Apr 07, 2024 pm 05:57 PM

MongoDB is a document-oriented, distributed database system used to store and manage large amounts of structured and unstructured data. Its core concepts include document storage and distribution, and its main features include dynamic schema, indexing, aggregation, map-reduce and replication. It is widely used in content management systems, e-commerce platforms, social media websites, IoT applications, and mobile application development.

Where are the mongodb database files? Where are the mongodb database files? Apr 07, 2024 pm 05:42 PM

The MongoDB database file is located in the MongoDB data directory, which is /data/db by default, which contains .bson (document data), ns (collection information), journal (write operation records), wiredTiger (data when using the WiredTiger storage engine ) and config (database configuration information) and other files.

How to open mongodb How to open mongodb Apr 07, 2024 pm 06:15 PM

On Linux/macOS: Create the data directory and start the "mongod" service. On Windows: Create the data directory and start the MongoDB service from Service Manager. In Docker: Run the "docker run" command. On other platforms: Please consult the MongoDB documentation. Verification method: Run the "mongo" command to connect and view the server version.

See all articles