search
HomeDatabaseMysql TutorialMongoDB的聚集操作

MongoDB的聚集操作

Jun 07, 2016 pm 03:58 PM
mongodboutintroductionoperatepolymerization

聚合引言 聚集操作就是出来数据记录并返回计算结果的操作。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
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
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

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

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.