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