Demand:
A colleague from the off-duty operation sent a requirement to count the number of occurrences of a field (_ID) in all document in a collection in the database. The range of the _id field is 0-4000.
Suppose collection is like this:
{ 123},{ 456},{ _id:123}
So the end result: [{_id:123, value:2}, {_id:456, value:1}] is actually a count frequency operation
Scheme:
Rough methods can traverse all document and accumulate, but the better way is to use MongoDB's MapReduce operation: https://docs.mongodb.com/manual/core/map-reduce/
db.collection.mapReduce ( function() {Emit (this. _id, 1)}, function(key, values) {return array.sum (values);}, 1}} )
It is worth mentioning that emit, value is 1, so that each document is a 1, then reduce when we get a full 1 array.
Next, using the method mentioned by http://www.cnblogs.com/agentgamer/p/4994650.html, can be inherited into the shell, the standard output to other tools to use, the output of the inline is basically a JSON.
One MongoDB statistic requirement