MONGODB group operations and pipe aggregate group sorting paging __mongodb

Source: Internet
Author: User
Tags mongodb

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) {...}")









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.