Size 19103932 is larger than MaxDocumentSize 16777216

I have some code the generates a series of BsonDocuments and tries to insert them into a Mongo Collection. On one of the documents, I get this error.

I can see that the size is larger than the reported MaxDocumentSize, but if I execute

db.isMaster().maxMessageSizeBytes

directly towards the Mongo server, then et reports 48000000, which is significantly higher than the one reported in the error message.

I’m using the .net driver 2.11.3 (and have tried updating to 2.12.4 without success).

Is there a way to somehow compress the document being sent - like removing whitespaces in the json document?

Hello @Benny_Skjold_Tordrup,

The error message “Size 19103932 is larger than MaxDocumentSize 16777216” is refering to the maximum size of a MongoDB document that can be stored in a collection - which is 16 Megabytes. See Document Size Limit. Since the document you are trying to insert has more than 16 MB, there is the error.

The db.isMaster is related to replica sets, and the command returns a document that describes the role of the mongod instance. The db.isMaster.maxMesageSizeBytes refers to - The maximum permitted size of a BSON wire protocol message. The default value is 48000000 bytes. This is not related to the error message you are getting.

1 Like

What do I then do to get this document stored? It is not an option so split it up. The document has this structure:

/* 1 createdAt:6/17/2021, 9:14:01 AM*/
{
	"_id" : ObjectId("60caf639b13fa2d7f0678ee8"),
	"id" : 422699,
	"sourceId" : "Airports-535-1",
	"origin" : "Miljøstyrelsen",
	"noiseClass" : "E",
	"noiseInterval" : 5,
	"date" : "2016-12-19T00:00:00",
	"hasGeometry" : true,
	"geometry" : {
		"type" : "MultiPolygon",
		"coordinates" : [
			[
			... bunch of coordinates 
			]
		]
	},
	"square" : {
		"Type" : "Polygon",
		"Coordinates" : [
			[
				[
					12.39719995332741,
					55.445833896222
				],
				[
					12.55498453345486,
					55.44134688006085
				],
				[
					12.563078945878638,
					55.53103046866473
				],
				[
					12.404936833922546,
					55.53553246668553
				],
				[
					12.39719995332741,
					55.445833896222
				]
			]
		]
	},
	"areaLevel" : 60,
	"last_updated" : ISODate("2021-06-17T09:14:08.345+02:00")
},

where the “bunch of coordinates” refer top a list of GeoJson coordinates.

What is the reason for the 16 Mb max document size?

Can documents be compressed somehow? What serializer settings are used for serializing the json document?

I believe 16 MB is an optimal size for a document to be stored in a collection. I think, for most practical purposes it suffices. For larger data sizes, usually media data like video files or images, GridFS allows large sized storage.

In general, the data stored is modeled (or designed) in such a way so as to store, query (also perform other operations) and use it efficiently in an application. This topic is called as Data Modeling. Note that data storage requires disk drive space and while working with data requires RAM memory. An efficient model takes into consideration various factors like - number of documents, the operations on them, size of a document, storage / memory requirements, etc.


Can documents be compressed somehow?

No, the document size limit applies.



Reference: