我想用mongo的聚合算个uv,但下面的命令,如图:
报错,aggregation result exceeds maximum document size (16MB)
文档中提到这个问题如图, http://docs.mongodb.org/manual/core/aggregation-pipeline-limits/,
请各位帮忙看看
感谢
天蓬老师2017-04-22 09:01:32
Too much data and can’t be processed. You can use $project to select the next field first and then group it,
{$project:{
xxx : 1,
yyy : 1,
}
}...
PHPz2017-04-22 09:01:32
The syntax is correct. I tried the following data on my machine:
> db.uv.find()
{ "_id" : ObjectId("52a102490e085e51aa153478"), "ip" : "127.0.0.1" }
{ "_id" : ObjectId("52a1024b0e085e51aa153479"), "ip" : "127.0.0.2" }
{ "_id" : ObjectId("52a1024c0e085e51aa15347a"), "ip" : "127.0.0.2" }
{ "_id" : ObjectId("52a1024e0e085e51aa15347b"), "ip" : "127.0.0.3" }
In addition to match, I use the following aggregation query:
db.uv.aggregate({
$group: {
_id: "$ip",
uv: {
$first: 1
}
}
}, {
$group: {
_id: "result",
uv: {
$sum: "$uv"
}
}
})
The result is correct
{ "result" : [ { "_id" : "result", "uv" : 3 } ], "ok" : 1 }
Because the final result is only one number, there should not be such an error. For debugging purposes, it is recommended to output the results of each step in the pipeline. Add { $limit: 3 }
after the step to be output, do not run the subsequent steps, and run the output. If there is too much data and the result is too slow, add a limit at the beginning and run it on small data first.
Change it again and again and run it in the shell. If it is inconvenient, just write it to a file, in terminal
in
$ mongo < agg_uv.js
Input redirection cannot be omitted, otherwise it will run in script mode, which is somewhat different from interactive mode.