Skip to content

Instantly share code, notes, and snippets.

@barisusakli
Last active July 11, 2023 14:53
Show Gist options
  • Save barisusakli/f8639e2a9554ee93b417 to your computer and use it in GitHub Desktop.
Save barisusakli/f8639e2a9554ee93b417 to your computer and use it in GitHub Desktop.
Get biggest document in mongo
#mongodb queries
db.objects.find({ $and: [
{ _id: { $gt: ObjectId("5b06052649f0a0999558b960")} },
{ _key: { $not: /^anal.*/ } },
{ _key: { $not: /^group.*/ } },
{ _key: { $not: /^user/ } },
{ _key: { $not: /^post/ } },
{ _key: { $not: /^topic/ } },
{ _key: { $not: /^chat/ } },
{ _key: { $not: /^follow/ } },
{ _key: { $not: /^category/ } },
{ _key: { $not: /^categories/ } },
{ _key: { $not: /^message/ } },
{ _key: { $not: /^uid/ } },
{ _key: { $not: /^tid/ } },
{ _key: { $not: /^cid/ } },
{ _key: { $not: /^pid/ } },
{ _key: { $not: /^ip/ } },
{ _key: { $not: /^flag/ } },
{ _key: { $not: /^fullname/ } },
{ _key: { $not: /^appId:uid/ } },
{ _key: { $not: /^diff/ } },
{ _key: { $not: /^banned/ } },
{ _key: { $not: /^upload/ } },
{ _key: { $not: /^event/ } },
] }).sort({_id: 1});
// biggest object
var max = 0;
var key = '';
db.objects.find().forEach(function(obj) {
var curr = Object.bsonsize(obj);
if(max < curr) {
max = curr;
key = obj._key;
}
})
print(key);
print(max);
//execute with:
//mongo localhost:27017/<db_name> doc_size.js
// group by keys
db.objects.aggregate([
{ $group: { _id: { _key: '$_key'}, count: { $sum: 1 } } } ,
{ $sort: { count: -1 } },
{ $limit: 100 },
], { "allowDiskUse" : true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment