The basic aggregation function of data aggregation in MongoDB count, distinct, group

Source: Internet
Author: User

In the mapreduce> of data aggregation in the previous article <mongodb, we mentioned a way to perform data aggregation operations in MongoDB--mapreduce, but in most daily use processes, We do not need to use MapReduce to operate, or a bit overkill feeling, in this article, we will simply say with the aggregation function of the data aggregation of the implementation of the operation.


There are three basic aggregation functions in MongoDB: count, distinct, and group. Let's take a look at these three basic aggregate functions separately.


(1) Count

Role: The number of documents in a simple statistics collection that meet a certain condition.

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 want to limit the maximum number of documents to check out, or if you want to count and skip the specified number of documents, you also need to use Limit,skip.

Example:

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

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


(2) distinct

function: Used to de-process the document needles in the collection

How to use: Db,collection.distinct (field,query)

Parameter description: field is a go-to-heavy fields, can be a single field name, or it can be a nested field name; query is the search condition and can be empty;

Example:

Db.collection.distinct ("user", {"age": {$gt: 28}})//used to query for different usernames older than 28 years old

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

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

CollectionName: The set name of the redo statistic, Distinctfield: The,,<query> of the Redo field is an optional limiting condition;

Example:


The difference between the two approaches is that the first method is to encapsulate the second method, the first one only returns the value collection of the field values after the redo statistic, but the second method returns both the field value collection and the detail information when the statistic is returned.


(3) Group

Function: Used to provide richer statistical requirements than count and distinct, you can use the JS function to control statistical logic

How to use: Db.collection.group (Key,reduce,initial[,keyf][,cond][,finalize])

Note: Before the 2.2 version, the group operation can only return a maximum of 10,000 packet records, but from 2.2 to 2.4 versions, MongoDB has been optimized to support the return of 20,000 grouped records returned, if the number of grouped records is more than 20,000, then you may need to be counted in other ways , such as converged pipelines or mapreduce;


The above three kinds of three kinds of aggregation functions in MongoDB are simply described, and the need to pay attention to the place has a simple explanation, if you need to use deep, you can go to the MongoDB website to view the details of the relevant information, thank you.



The basic aggregation function of data aggregation in MongoDB count, distinct, group

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.