MongoDB Aggregate use personal summary _mongodb

Source: Internet
Author: User
Tags mongodb mongodb aggregate neo4j

Recently has been using MongoDB, sometimes will need to use statistics, on the Internet to check some information, the most suitable for use is to use aggregate, the following introduction of their own use of experience.

MongoDB polymerization
MongoDB Aggregation (aggregate) is used primarily to process data (such as statistical averages, sums, and so on) and to return computed data results. Somewhat similar to the count (*) in an SQL statement.
Aggregate () method
Methods of aggregation in MongoDB use aggregate ().
Grammar
The basic syntax format for the aggregate () method is as follows:

Db. Collection_name.aggregate (aggregate_operation)
instance

The data in the collection is as follows:

{
  _id:objectid (7df78ad8902c)
  title: ' MongoDB Overview ', 
  Description: ' MongoDB is no SQL database ', by
  _user: ' jb51.net ',
  URL: ' http://www.jb51.net ',
  Tags: [' mongodb ', ' database ', ' NoSQL '],
  likes:100
},
{
  _id:objectid (7df78ad8902d)
  title: ' NoSQL Overview ', 
  Description: ' No SQL database is very fast ',
  by_user: ' jb51.net ',
  URL: ' http://www.jb51.net ',
  Tags: [' mongodb ', ' database ', ' NoSQL '],
  likes : Ten
},
{
  _id:objectid (7df78ad8902e)
  title: ' neo4j Overview ', 
  Description: ' neo4j is no SQL Database ',
  by_user: ' neo4j ',
  URL: ' http://www.neo4j.com ',
  Tags: [' neo4j ', ' database ', ' NoSQL '],
  likes:750
},

Now we compute the number of articles written by each author through the collection above, using aggregate () to compute the results as follows:

> db.mycol.aggregate ([{$group: {_id: $by _user, num_tutorial: {$sum: 1}}}])
{"Result
  ": [
   {
     "_id" : "w3cschool.cc",
     "num_tutorial": 2
   },
   {
     "_id": "neo4j",
     "num_tutorial": 1
   }
  ],
  "OK": 1
}
>

The above instance is similar to the SQL statement: Select By_user, COUNT (*) from MyCol GROUP by By_user
In the example above, we group the data through the field By_user field and compute the sum of the same values in the By_user field.
The following table shows some of the aggregated expressions:

An expression Description instance
$sum Calculates the sum. Db.mycol.aggregate ([{$group: {_id: "$by _user", num_tutorial: {$sum: "$likes"}}])
$avg Calculate average Db.mycol.aggregate ([{$group: {_id: "$by _user", num_tutorial: {$avg: "$likes"}}])
$min Gets the minimum value for all documents in the collection. Db.mycol.aggregate ([{$group: {_id: "$by _user", num_tutorial: {$min: "$likes"}}])
$max Gets the corresponding maximum value for all documents in the collection. Db.mycol.aggregate ([{$group: {_id: "$by _user", num_tutorial: {$max: "$likes"}}])
$push Inserts a value into an array in the resulting document. Db.mycol.aggregate ([{$group: {_id: "$by _user", url: {$push: "$url"}}])
$addToSet Inserts a value into an array in the resulting document, but does not create a copy. Db.mycol.aggregate ([{$group: {_id: "$by _user", url: {$addToSet: "$url"}}])
$first Gets the first document data based on the sort of resource document. Db.mycol.aggregate ([{$group: {_id: "$by _user", First_url: {$first: "$url"}}])
$last Get the last document data based on the sort of resource document Db.mycol.aggregate ([{$group: {_id: "$by _user", Last_url: {$last: "$url"}}])

The concept of piping
Pipelines are typically used in UNIX and Linux to take the output of the current command as an argument to the next command.
The MongoDB aggregation pipeline passes the MongoDB document to the next pipe processing after it has been processed in one pipe. The pipe operation can be repeated.
Expression: Process input document and output. The expression is stateless, can only be used to compute the document for the current aggregation pipeline, and cannot process other documents.
Here are a few of the common operations in the aggregation framework:
$project: Modify the structure of the input document. Can be used to rename, add, or delete fields, or to create calculations and nested documents.
$match: For filtering data, output only documents that match the criteria. $match the standard query operation using MONGODB.
$limit: Used to limit the number of documents returned by the MongoDB aggregation pipeline.
$skip: Skips the specified number of documents in the aggregation pipeline and returns the remaining documents.
$unwind: Splits one of the array type fields in a document into multiple bars, each containing one of the values in the array.
$group: Groups The documents in the collection and can be used for statistical results.
$sort: The input document is sorted and output.
$geoNear: Output an ordered document close to a geographic location.

Pipe operator Instance

1. $project examples

Db.article.aggregate (
  {$project: {
    title:1,
    author:1,
  }}
 );

In this case, there will only be _id,tilte and author three fields, by default _id fields are included, if you want to include _id words:

Db.article.aggregate (
  {$project: {
    _id:0,
    title:1,
    author:1
  }});

2. $match examples

Db.articles.aggregate ([
            {$match: {score: {$gt: $lte:}}},
            {$group: {_id:null, Count: {$sum: 1 } } }
            ] );

The $match is used to get the score greater than 70 is less than or equal to 90 records, and then the qualifying records are sent to the next stage $group pipeline operator for processing.

3. $skip examples

Db.article.aggregate (
  {$skip: 5});

After the $skip pipe operator, the first five documents were "filtered" out.

Other people have written I do not describe too much, we can search to find more than n the same, I write my summary.

Basic Knowledge

Please find out more on your own and here are the key documents.

Operator Introduction:

$project: Include, exclude, rename, and display fields
$match: Query, requires the same parameters as find ()
$limit: Limit the number of results
$skip: Ignore number of results
$sort: Sort results by the given fields
$group: Combining results by given expression
$unwind: Dividing embedded arrays into their own top-level files


Documentation: MongoDB official aggregate description.

Related use:

Db.collection.aggregate ([array]);

Array is any one or more operators.
Group and match usage, using the use of Sqlserver,group is very good to understand, according to the specified column for grouping statistics, you can count the number of groups, but also to statistical grouping and or average.
Match before group is a query on the source data, and match after group is a filter for the data after group;

Similarly, Sort,skip,limit is the same principle;

 {_id:1,name: "A", status:1,num:1}
 {_id:2,name: "A", status:0,num:2}
 {_id:3,name: "B", status:1,num:3}
 {_id:4,name: "C", Status:1,num:4}
 {_id:5,name: "D", status:1,num:5}

Here is an example:
Apply one: Statistics the number and total number of name;

Db.collection.aggregate ([
{$group: {_id: "$name", count:{$sum: 1},total:{$sum: "$num"}}
]);

Application Two: Statistics the number of Status=1 name;

Db.collection.aggregate ([
{$match: {status:1}},
{$group: {_id: ' $name ', count:{$sum: 1}}}
]);

Application Three: Statistics the number of name, and the number is less than 2;

Db.collection.aggregate ([
{$group: {_id: "$name", count:{$sum: 1}},
{$match: {count:{$lt: 2}}
]);

Application IV: Statistics Stauts=1 number of name, and the number is 1;

Db.collection.aggregate ([
{$match: {status:1}},
{$group: {_id: ' $name ', count:{$sum: 1}}},
{$match: { Count:1}
]);

Multiple column group, multiple columns based on name and status

Db.collection.aggregate ([
{$group: {_id:{name: "$name", St: "$status"},count:{$sum: 1}}}
);

$project the operator is simple,

Db.collection.aggregate ([
{$project: {name:1,status:1}}
]);

As a result, only table data _id,name,status three fields, equivalent to the SQL expression select _id,name,status from collection

$unwind
This operator can split an array of documents into multiple documents, in special conditions useful, I do not do too much research.

The above basic can achieve most statistics, group former conditions, group after conditions, is the focus.

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.