Home Database Mysql Tutorial MongoDB的聚集操作

MongoDB的聚集操作

Jun 07, 2016 pm 03:58 PM
mongodb out introduction operate polymerization

聚合引言 聚集操作就是出来数据记录并返回计算结果的操作。MongoDB提供了丰富的聚集操作,能够检测和执行数据集上的计算。运行在mongod上的数据聚集简化了代码和资源限制。 像查询一样,在Mongo的聚合操作使用collections作为输入,并返回一个或多个document

聚合引言

聚集操作就是出来数据记录并返回计算结果的操作。MongoDB提供了丰富的聚集操作,能够检测和执行数据集上的计算。运行在mongod上的数据聚集简化了代码和资源限制。

像查询一样,在Mongo的聚合操作使用collections作为输入,并返回一个或多个document作为输出。

聚合模式

聚合管道

MongoDB2.2引入了一个新的聚合框架:聚合管道,这是基于数据处理管道概念的模型。文档输入一个多阶段的管道并将文档转化为一个聚合的结果。

最基本的管道阶段提供了过滤器(Filters)来像查询一样操作,和文档转化(Document transformations)来修改输出文档的形式。

其他的管道提供了对特定字段分组和排序的工具,也有聚合数组内容的工具。并且,管道阶段能对任务使用操作符,比如计算平均值或连接一个字符串。

管道使用MongoDB原生的操作提供了有效的数据聚合,也是MongoDB里面一个理想的数据聚合方法。

\

上面带有注释的聚合操作管道操作,这个聚合管道有两个阶段: $match和$group

Map-Reduce

MongoDB也提供了map-reduce操作来执行聚合操作。一般的,map-reduce操作有两个阶段:Map阶段来处理每个文档并为每个输入文档输出一个或多个文档,Reduce阶段整合map操作的输出。Map-reduce有一个可选的finalize阶段,来为结果做最终的修改。和其他聚合操作一样,map-reduce指出了查询条件来选择输入文档并排序和限制结果。

Map-reduce使用自定义的javasript函数来执行map和reduce操作,以及可选的finalize操作。虽然自定义的javasript相对聚合管道提供了更大的灵活性,但一般map-reduce相对低效和复杂。

并且,map-reduce操作的输出集合比集合管道的输出极限大16M。

注:

从MongoDB2.4开始,特定的mongo shell函数和属性不能被map-reduce操作使用。MongoDB2.4也提供了同时执行多个Javascript操作的支持。在MongoDB2.4之前,javascript代码只能在单线程执行,这为map-reduce的并行带来困难。

\

单一目的的聚合操作

对很多的常规单一目的的聚合操作(single purpose aggregation operation),MongoDB提供了特殊目的的数据库命令。这些常规的聚合操作是:返回匹配文档的数量,返回一个字段的唯一的值,和基于一个字段值的分组。所有这些操作都是来自一个collection。虽然这些操作提供了简单的常规聚合操作处理方法,但他们都缺乏灵活性和像聚合管道、Map-reduce那样的能力。

\

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)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months 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)

Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Jun 25, 2024 pm 07:09 PM

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

How to connect navicat to mongodb How to connect navicat to mongodb Apr 24, 2024 am 11:27 AM

To connect to MongoDB using Navicat, you need to: Install Navicat Create a MongoDB connection: a. Enter the connection name, host address and port b. Enter the authentication information (if required) Add an SSL certificate (if required) Verify the connection Save the connection

What is the use of net4.0 What is the use of net4.0 May 10, 2024 am 01:09 AM

.NET 4.0 is used to create a variety of applications and it provides application developers with rich features including: object-oriented programming, flexibility, powerful architecture, cloud computing integration, performance optimization, extensive libraries, security, Scalability, data access, and mobile development support.

Integration of Java functions and databases in serverless architecture Integration of Java functions and databases in serverless architecture Apr 28, 2024 am 08:57 AM

In a serverless architecture, Java functions can be integrated with the database to access and manipulate data in the database. Key steps include: creating Java functions, configuring environment variables, deploying functions, and testing functions. By following these steps, developers can build complex applications that seamlessly access data stored in databases.

How to configure MongoDB automatic expansion on Debian How to configure MongoDB automatic expansion on Debian Apr 02, 2025 am 07:36 AM

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

How to ensure high availability of MongoDB on Debian How to ensure high availability of MongoDB on Debian Apr 02, 2025 am 07:21 AM

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

Navicat's method to view MongoDB database password Navicat's method to view MongoDB database password Apr 08, 2025 pm 09:39 PM

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).

Major update of Pi Coin: Pi Bank is coming! Major update of Pi Coin: Pi Bank is coming! Mar 03, 2025 pm 06:18 PM

PiNetwork is about to launch PiBank, a revolutionary mobile banking platform! PiNetwork today released a major update on Elmahrosa (Face) PIMISRBank, referred to as PiBank, which perfectly integrates traditional banking services with PiNetwork cryptocurrency functions to realize the atomic exchange of fiat currencies and cryptocurrencies (supports the swap between fiat currencies such as the US dollar, euro, and Indonesian rupiah with cryptocurrencies such as PiCoin, USDT, and USDC). What is the charm of PiBank? Let's find out! PiBank's main functions: One-stop management of bank accounts and cryptocurrency assets. Support real-time transactions and adopt biospecies

See all articles