db.getCollection('docs').aggregate([
{$match: {}},
{$group: {'_id':"$name", num:{$sum:1} }},
{$sort:{num:-1}}, { $limit : 50}
])The opposite would be to count how many unique records exists for a given field:
db.getCollection('docs').distinct('name')db.getCollection('docs_metadata').find({size: {$exists: true}, name: {$regex: /.+pdf/i}}).sort({size: -1}).limit(5)var maxSize = 1024;
var bigDocs = 0;
var avgSize = 0;
var count = 0;
db.getCollection('docs_metadata').find({}).forEach(
function (doc) {
count++;
var docSize = Object.bsonsize(doc);
avgSize += docSize;
if (docSize >= maxSize) {
bigDocs++;
// print(doc._id + ' is ' + docSize + ' bytes');
}
}
)
print(`Found ${bigDocs} (out of ${count}) documents bigger than ${maxSize} bytes.\nAverage doc size: ${avgSize / count}`);
Uh oh!
There was an error while loading. Please reload this page.