Aggregation operations

Perform statistical analysis on the data in the returned documents.

Polymerization pipe

 db.users.aggregate([
     {$group:{_id:"$name",uage:{$sum:"$age"}}}]) {"_id" : "lucy"."uage": 10} {"_id" : "tom"."uage": 35} {"_id" : [ "a"."b"."c"]."uage": 23} {"_id" : "jack"."uage": 47} {"_id" : [ "b"."c"]."uage" : 0 }

//$group_ID indicates the value of a grouped field, and uage indicates the aggregated key. Value indicates the field value to be aggregated //$matchRepresents the matching pipe,$groupPacket pipe,$projecProjection pipe,$skip.$limit.$sort
//$sum.$avg.$min.$max.$first.$last/ / {$sum:1} Example for counting the number of all documents >db.users.aggregate([{$match: { $or: [ { age: { $gt: 70, $lt: 90 } }, { v: { $gte: 1000}}]}}, {$group: { _id: "$name", newKey: { $sum: 1}}}]); Db.products. Aggregate ([{db.products. Aggregate ([{db.products.$match:{quantity:{$lt: 10}}}, {$sort:{quantity:1}},{$skip: 1}, {$limit:3}]) // Matching, sorting, pagingCopy the code

Aggregation implements sorting, skipping 1 item and returning 3 items (i.e. paging) as follows:

// All data
{ "_id" : ObjectId("617929cecab76e646e11e7b4"), "quantity" : 2."price" : 4."pnumber" : "p001" }
{ "_id" : ObjectId("6179295dcab76e646e11e7b0"), "quantity" : 2."price" : 5."pnumber" : "p003" }
{ "_id" : ObjectId("61792970cab76e646e11e7b2"), "quantity" : 2."price" : 8."pnumber" : "p002" }
{ "_id" : ObjectId("617929dfcab76e646e11e7b5"), "quantity" : 4."price" : 10."pnumber" : "p003" }
{ "_id" : ObjectId("61792a0ccab76e646e11e7b8"), "quantity" : 5."price" : 10."pnumber" : "p002" }
{ "_id" : ObjectId("617929f8cab76e646e11e7b6"), "quantity" : 10."price" : 20."pnumber" : "p001" }
{ "_id" : ObjectId("617929fbcab76e646e11e7b7"), "quantity" : 10."price" : 20."pnumber" : "p003" }

> db.products.aggregate([{$sort: {price:1}}, {$skip:1}, {$limit:3}])//price sort, skip 1, select 3

{ "_id" : ObjectId("617929cecab76e646e11e7b4"), "quantity" : 2."price" : 4."pnumber" : "p001" }
{ "_id" : ObjectId("6179295dcab76e646e11e7b0"), "quantity" : 2."price" : 5."pnumber" : "p003" }
{ "_id" : ObjectId("61792970cab76e646e11e7b2"), "quantity" : 2."price" : 8."pnumber": "p002" }
Copy the code

Graphs operation

>db.users.find()
{ "_id" : ObjectId("611dc14a623a0dbae82982c5"), "name" : "jack"."age": 29} {"_id" : ObjectId("611dc14a623a0dbae82982c6"), "name" : "tom"."age": 23} {"_id" : ObjectId("611dd190d0bf16d55c366a29"), "name" : "jack"."age": 18} {"_id" : ObjectId("611dd1aad0bf16d55c366a2a"), "name" : "tom"."age": {12}"_id" : ObjectId("611dd1aad0bf16d55c366a2b"), "name" : "lucy"."age": 10} // Use mapreduce to perform aggregation. Age >var mapFunc=function() {... emit(this.name,this.age); // define a map function, grouping the map function by name, name as key, and age as new value, resulting in the following result: "jack" :[29,18,]... } > var reduceFunc=function(key,values){
... returnArray.sum(values); // array.avg () // array.min () // array.tab } > db.users.mapReduce(mapFunc,reduceFunc,{out:{replace:"map-reduce-result"{}})"result" : "map-reduce-result"."ok": 1}Copy the code

MongoDB—- Database, collection, document operation next: MongoDB index