2

Knowing that mongoDB has 16MB data size per document, we are using file system to store such data, but some how in mongodb, there are bson date larger than 16 mb, as a part of correction, we want to identify those _id and delete it. how can we find the id that has more then 16mb bson size.

I can find the largest size of the document from the following query.. But want to find the number of large size documents in the collection

var max = 0, id = null;
db.Email_Notification_log.find().forEach(doc => {
    const size = Object.bsonsize(doc);
    if(size > max) {
            max = size;
            id = doc._id;
       }
    });
print(id, max);

It is mongodb ver 4.0.4 and its a standalone, it is crashing the mongodb service when i run this query..

joel Raja
  • 59
  • 5
  • 1
    This seems to be a moot point, because BSON documents cannot be larger than 16MB. If they seem larger, maybe it is because of some extra metadata etc. The only check you need to worry about is making sure you don't exceed 16MB of data for an intended single Mongo document. – Tim Biegeleisen Jul 29 '20 at 06:04
  • yes, now we are making sure, the data not exceed 16MB, – joel Raja Jul 29 '20 at 06:14
  • See the answer in this post: [How to find the size of the specific Document in MonogoDB Collection?](https://stackoverflow.com/questions/48960694/how-to-find-the-fhe-size-of-the-specific-document-in-monogodb-collection) – prasad_ Jul 29 '20 at 06:30
  • yes Object.bsonsize is useful to find a size of the document, i was able to find the largest size of the document using the following query. – joel Raja Jul 29 '20 at 09:19
  • var max = 0, id = null; db.Email_Notification_log.find().forEach(doc => { const size = Object.bsonsize(doc); if(size > max1) { max = size; id1 = doc._id; } }); print(id, max); – joel Raja Jul 29 '20 at 09:19
  • now, i want to understand how many are there.. – joel Raja Jul 29 '20 at 09:19
  • Instead of replacing `id1`, put the ids (for which bsonsize is > 16mb) into an array. – Sergio Tulentsev Jul 29 '20 at 09:38
  • Thanks @SergioTulentsev, i will have to modify it accordingly.. – joel Raja Jul 30 '20 at 14:45

1 Answers1

1

MongoDB Documentation states:

The maximum BSON document size is 16 megabytes. The maximum document size helps ensure that a single document cannot use excessive amount of RAM or, during transmission, excessive amount of bandwidth. To store documents larger than the maximum size, MongoDB provides the GridFS API. See mongofiles and the documentation for your driver for more information about GridFS.

https://docs.mongodb.com/manual/core/document/#document-size-limit

Yahya
  • 3,386
  • 3
  • 22
  • 40