To get data in groups:
Db.express_info.group ({"key": {"Express_code": true}, "initial": {"num": "0", "mobile": "0"}, "reduce": function (Doc, Result) {result.num++, result.mobile=doc.mobile}, ' condition ': {' mobile ': ' 18663930231 '}, ' Finalize ': function (Result ) {result.is_push=result.num+1}})
Analysis:
Key: Columns used in groups
Initial: Set initial return element
Reduce:doc is the document in the collection, and result is the initial initial
Condition: Query Criteria
Finalize: Process a data document for a group before returning results
Return Data:
[
{
"Express_code": "538419969049", "
num": ","
mobile ":" 18663930231 ",
" Is_push ":
]
Pipe Grouping Sorting paging:
Db.express_info.aggregate ([{$match: {"mobile": "18663930231"}},{$group: {_id: "$express _code", date_time:{$first: "$ DateTime "},express_code:{$first: $express _code"}, Num_tutorial: {$sum: 1}}},{$sort: {"datetime": -1}},{$skip: 5},{$ Limit:5}])
Analysis:
Match: Query criteria
Group: Grouping
_ID: Grouping conditions
DateTime: Gets the Datetime,express_code of the first piece of data in the group empathy
Num_tutorial: Additional content for statistical data in the group
Sort: Sorting
Skip: How many jumps?
Limit: Limit the number of bars.
Return Data:
[
{"_id": "538419968955", "Date_time": "2018-01-15 14:33:44", "Express_code": "538419968955", "num_tutorial": 17 }
{"_id": "812691219127", "Date_time": "2018-01-16 16:17:57", "Express_code": "812691219127", "Num_tutorial": 7}
{"_id": "123123123123", "Date_time": "2018-01-25 14:42:37", "Express_code": "123123123123", "num_tutorial": 1}< c3/>]
Ps:pymongo when performing a function method, you need to introduce
From Bson.code Import code
run_reduce = Code ("function (Doc,result) {...}")