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}`);
To install
mongo tools:wget wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-ubuntu1804-x86_64-100.7.5.debsudo apt install ./mongodb-database-tools-*-100.7.5.debTo get the download link visit mongoDB tool download page