Sort first, then limit the number of records, and then calculate the sum of the specified fields

Source: Internet
Author: User

MONGO xxxx:27017

Mongo 127.0.0.1:27017 (localhost)
Show databases
Use XX (database)
Db
Show collections

No result//db.proccessedfile.aggregate ([{$match: {datanumber:{$gte: 1, $lte: 9}}},{$group: {_id:null,sum:{$sum: "$ Datanumber "}}}])
No result//db.proccessedfile.aggregate ([{$match: {datanumber:{$lte: 9}}},{$group: {_id: ' $sum ', sum:{$sum: ' $dataNumber '}} }])
Db.proccessedfile.aggregate ([{$match: {datanumber:{$gte: 1}}},{$group: {_id:null,sum:{$sum: ' $dataNumber '}}])
Db.proccessedfile.aggregate ([{$limit: 9},{$group: {_id:null,sum:{$sum: ' $dataNumber '}}])
Db.proccessedfile.aggregate ([{$sort: {processeddate:-1}},{$limit: 2},{$group: {_id:null,sum:{$sum: ' $dataNumber '}} }])

{$sort: {_id:1}}], {cursor:{batchsize:0},explain:true}, aggregate append, $group,

Example: Db.test1.aggregate ([{$match: {status: "A"}},{$group: {_id: "$cust _id",

max_amout:{$max: "$amount"}}},{$sort: {_id:1}}], {cursor:{batchsize:0},explain:true});
**

Db.proccessedfile.aggregate ([{$sort: {processeddate:-1}},{$limit: 2},{$group: {_id:null,sum:{$sum: ' $dataNumber '}} }])
Sort first, then limit the number of records, and then calculate the sum of the specified fields
Db.getcollection (' Proccessedfile '). Aggregate ([{$sort: {processeddate:1}},{$limit: 400},{$group: {_id:null,sum:{$ Sum: "$dataNumber"}}}])

Db.proccessedfile.aggregate ([{$match: {datanumber:{$gte: 1, $lte: 9}}},{$group: {_id:null,sum:{$sum: ' $dataNumber ' }}}])
Db.proccessedfile.aggregate ([{$match: {datanumber:{$lte: 9}}},{$group: {_id:null,sum:{$sum: ' $dataNumber '}}])
Db.proccessedfile.find (). Sort ({"Processeddate":-1}). Limit (9)
var cursor = Db.proccessedfile.find (). Sort ({"Processeddate":-1}). Limit (9)

Cursor.foreach (function (doc) {Printjson (doc);});
Cursor.hasnext ()
for () {
var doc = Cursor.next ();
Printjson (DOC);

}

var map = function () {emit (This._id,this.datanumber);}
var reduce = function (_id,datanumber) {return {_id:_id,datanumber:datanumber}}
Db.runcommand ({
MapReduce: ' Proccessedfile ',
Map:map,
Reduce:reduce,
Query:this.limit (9),
Out: ' Res2 '
})
Db.runcommand ({mapReduce: ' Proccessedfile ', Map:map, Reduce:reduce, out:{inline:1}})

Photo Supplement:

Note (_id corresponding value can be specified directly as a null value)

$group:

_id required, "$Acnt _id" is the specified statistic field #sum a random output field name $sum sum "$_id" the sum of the fields
When $group, you must specify a _id field, and you can include some expression operators of arithmetic types:
Db.article.aggregate (
{$group: {
_id: "$author",
Docsperauthor: {$sum: 1},
Viewsperauthor: {$sum: "$pageViews"}
}});

Note: 1. The output of the $group is unordered.
2. $group operation is currently in memory, so it cannot be used to group a large number of documents.

Sort first, then limit the number of records, and then calculate the sum of the specified fields

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.