MongoDB aggregate functions count, DISTINCT, group how to implement data aggregation operations _mongodb

Source: Internet
Author: User
Tags mongodb mongodb aggregate

This article introduced the MongoDB in the MapReduce implementation of data aggregation method, we mentioned MongoDB in the data aggregation operation of a way--mapreduce, but in most of the day-to-day use of the process, We do not need to use MapReduce to do the operation. In this article, we simply talk about the implementation of the data aggregation operation with the aggregation function.

In addition to the basic query capabilities, MONGODB provides powerful aggregation capabilities. There are three basic aggregate functions that are brought in MongoDB: count, distinct, and group. Let's talk about these three basic aggregate functions separately.

(1) Count

Function: Simple Statistics The number of documents in a collection that meet certain criteria.

How to use: Db.collection.count (<query>) or Db.collection.find (<query>). Count ()

Parameter description: Where <query> is the target condition for the query. If you have a document that you want to limit the maximum number of documents that you can find, or if you want to skip the specified number of documents, you also need to use Limit,skip.

Example:

Copy Code code as follows:

Db.collection.find (<query>). limit ();
Db.collection.find (<query>). Skip ();

(2) distinct

Role: Used to redo the document pins in the collection

Use mode: db,collection.distinct (field,query)

Parameter description: field is to go to the heavy field, can be a single field name, can also be nested field names, query is the search criteria, can be empty;

Example:

Db.collection.distinct ("user", {"age": {$gt: 28}});//For querying different user names of age older than 28 years

In addition to the above usage, you can use one of the following methods:

Db.runcommand ({"distinct": "CollectionName", "Key": "Distinctfied", "Query": <query>})

CollectionName: Go to the collection name of the statistics, Distinctfield: Go to the heavy field,,<query> is an optional restriction condition;

Example:

The difference between the two approaches is that the first method is the encapsulation of the second method, which returns only the collection of field values after the statistics, but the second way returns the details of the field value collection as well as the statistics.

(3) Group

Role: To provide more statistical requirements than count and distinct, you can use the JS function to control statistical logic

Use mode: Db.collection.group (Key,reduce,initial[,keyf][,cond][,finalize])

Note notes: The group operation can only return 10,000 group records before version 2.2, but from 2.2 to 2.4, MongoDB is optimized to support the return of 20,000 grouped records, and if the number of bars in the grouped records is greater than 20,000, then you may need to count other ways. , such as polymeric pipelines or mapreduce;

The above is a simple description of the three kinds of three aggregate functions in the MongoDB, and a simple description of the places needing attention, if you need further use, you can enter the MongoDB website to view the relevant details, thank you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.