Saturday 8 October 2016

com.mongodb.MongoInternalException: org.springframework.dao.InvalidDataAccessResourceUsageException

Exception in the mongoDB related document  size : 
------------------------------------------------------------------------------------------------------------------

com.mongodb.MongoInternalException: Size 19534568 is larger than MaxDocumentSize 16793600.
org.springframework.dao.InvalidDataAccessResourceUsageException: Size 19534568 is larger than MaxDocumentSize 16793600.; nested exception is com.mongodb.MongoInternalException: Size 19534568 is larger than MaxDocumentSize 16793600.
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:77)
at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2002)
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:460)
at org.springframework.data.mongodb.core.MongoTemplate.doUpdate(MongoTemplate.java:1077)
at org.springframework.data.mongodb.core.MongoTemplate.updateFirst(MongoTemplate.java:1051)
--------------------------------------------------------------------------------------------------------------------------------

Solution : Above mentioned exceptions occurred when your document size exceeds the default BSON size limit of 16 MB and MongoDB supports no more than 100 levels of nesting for BSON documents in Mongo 3.0. [reference]

Its being made limited to avoid excessive use of RAM or during data transfers avoiding excessive bandwidth  consumption of the network. But 16 MB is still good number to store documents in a collection. 

Note :  A record in a MongoDB collection and the basic unit of data in MongoDB. Documents are analogous to JSON objects but exist in the database in a more type-rich format known as BSON.

Going beyond storing the BSON threshold limit , if one require to store say 100 MB or 2.5 GB  size of file [as in case of Video, Audio streaming files etc] then MongoDB provides a way to solve this challenge.  GridFS  API feature is one of the main  and  brilliant feature of MongoDB. Using this one can store any huge size of data as its stored the file into chunks of 255KB and store the file in the DB.


When to Use GridFS

In MongoDB, use GridFS for storing files larger than 16 MB.
In some situations, storing large files may be more efficient in a MongoDB database than on a system-level filesystem.
  • If your filesystem limits the number of files in a directory, you can use GridFS to store as many files as needed.
  • When you want to access information from portions of large files without having to load whole files into memory, you can use GridFS to recall sections of files without reading the entire file into memory.
  • When you want to keep your files and metadata automatically synced and deployed across a number of systems and facilities, you can use GridFS. When using geographically distributed replica sets, MongoDB can distribute files and their metadata automatically to a number of mongod instances and facilities.
Do not use GridFS if you need to update the content of the entire file atomically. As an alternative you can store multiple versions of each file and specify the current version of the file in the metadata. You can update the metadata field that indicates “latest” status in an atomic update after uploading the new version of the file, and later remove previous versions if needed.
Furthermore, if your files are all smaller the 16 MB BSON Document Size limit, consider storing the file manually within a single document instead of using GridFS. You may use the BinData data type to store the binary data. See your drivers documentation for details on using BinData.


Check the Max size of BSON  : We can check it on Mongo Shell by issuing following command

db.isMaster().maxBsonObjectSize/(1024*1024);


Enjoy the coding and MongoDB!!!

No comments:

Post a Comment