- v1.10
Document
Basic Operation
Data Model
Data Model Examples
Database Management
Database Management
Backup & Recovery
Aggregation
Replication
Replication Concept
Replication Operations
Reference
Cluster
Cluster Architecture
Cluster Deployment
Sharding
Connector
Reference
Development
C Driver
C++ Driver
C# Driver
Java Driver
PHP Driver
Python Driver
FAQ
- Content
- Comments
- History
Aggregation Commands
$limit
$limitAchieved in the result set returned by limiting the number of reords. If the specified number of records is greater than the actual total number of records, then return the actual total number of records.
Example-
Limit the result set returned the first 10 records.
db.collectionspace.collection.aggregate( { $limit : 10 } )
This operation represents a collection of collection to read the first 10 records.
$skip
$skip Parameter controls the starting point of the result set, the result set that is skipping the number of records specified. If the number of records is greater than the total number of records to skip, returns 0 records.
Example-
Skip 10 records returned.
db.collectionspace.collection.aggregate( { $skip : 10 } ) ;
This operation represents a collection of collection of records read from and skip ahead 10, began to return from the first 11 records.
$sort
$sort Is used to specify the collation of the result set. Nested object using the dot operator(.) Reference field names.
Exampledb.collectionspace.collection.aggregate({$sort:{score:-1,name:1}});
This operation represents tead records from the collection collection in the field and score values were sorted in descending order (1 indicates ascendign, -1 means descending).
When the score between the record field values are the same, then the name field values in ascending order.
$match
$match can be achieved by matching criteria to select records from the collection.
cond parameter method introduced.
Example-
$match perform simple matching
db.collectionspace.collection.aggregate({ $match : {$and:[{score:80},{"info.name":{$exists:1}}]}})
This operation represents the return qualifying score from a set collection of =80 and the info object child object name field is present record.
-
$match matching records that meet that criteria, then
the result set using $group grouping final
output using $project focused to the specified
field name.
db.collectionspace.collection.aggregate({ $match : {$and:[{score:80},{"info.name":{$exists:1}}]}},{$group:{_id:"$major"}},{$project:{major:1,dep:1}})
The first set of collection operations in return qualifying score=80 and recording info objects child objects exist in the name field, and then grouped according to major fields, the final choice of major, and dep field output in the result set.
$project
$project operations can filter out the required fields from the record, the value is 1 if ghe field name, which means that elected to 0 means no election, can also be achieved rename fields.
-
$project quickly select from a focus on the results of field Related information required.
db.collectionspace.collection.aggregate({ $project : {title: 0,author: 1}})
This operation is selected author field, while title field in the result set is not output.
-
$project rename the field name, as follows:
db.collectionspace.collection.aggregate({ $project : {author: 1,name:"$studentName",dep:"$info.department"}})
This operation will rename the field names studentName name to output, the info object in the field of child objects department rename dep. Nested object field references using the dot operator (.) Point.
-
$match conditional matching records.
{ $match:{score:{$gt:80}}}
This output using $project and author of all score field values, and then press the $match record output matching conditions.$match $project must be selected field name.
$group
$group grouping to achieve the restult set, similar to the group by SQL statement. First, specify the grouping key(_id), by "_id" to identify the grouping field, the field can be a single packet, it can be more than one, the format is as follows:
Single packet key: {_id:"$field"} Multiple grouping key: {_id:{field1:"$field1",field2:"$field2",...}}
-
$group using the following:
db.collectionspace.collection.aggregate({$group:{_id:"$major",avg_score:{$avg:"$score"},Major:{$first:"$major"}}})
This operation reads the records from the collection represents the collection, grouped accrding to major fields. In the result set, taking the first record of each grouping of major fields, rename Major, field to score the value of each packet average, rename ave_score. Returns are as follows:{ "avg_score": 82, "major": "optics" } { "avg_score": 77.25, "major": "physics" }
$group Aggregation operator supports the following aggregate functions:
Function name | Description |
---|---|
$addtoset | Adds the specified field values in the array, the same field value will be added once. |
$first | Take packet field values in the first record. |
$last | Take packet field value in the record. |
$max | Take the maximum value of the packet field. |
$min | Take the smallest packet field values. |
$avg | Take the average packet field values. |
$push | Add all the fields to the array, even if the same array of field values that already exist, and continue to add. |
$sum | Take the sum of packet field values. |
$count | Return the number of all the records. |
- Latest Comment

2015-02-14