Find largest document size in MongoDB

前端 未结 7 548
耶瑟儿~
耶瑟儿~ 2020-12-01 01:33

Is it possible to find the largest document size in MongoDB?

db.collection.stats() shows average size, which is not really representative because in my

7条回答
  •  暖寄归人
    2020-12-01 01:43

    Note: this will attempt to store the whole result set in memory (from .toArray) . Careful on big data sets. Do not use in production! Abishek's answer has the advantage of working over a cursor instead of across an in memory array.

    If you also want the _id, try this. Given a collection called "requests" :

    // Creates a sorted list, then takes the max
    db.requests.find().toArray().map(function(request) { return {size:Object.bsonsize(request), _id:request._id}; }).sort(function(a, b) { return a.size-b.size; }).pop();
    
    // { "size" : 3333, "_id" : "someUniqueIdHere" }
    

提交回复
热议问题