The value of "offset" is out of range. It must be >= 0 && <= 17825792 The value of "offset" is out of range. It must be >= 0 && <= 17825792 mongoose mongoose

The value of "offset" is out of range. It must be >= 0 && <= 17825792


For those having the same issue. I have some extra information, after some testing I concluded the following things:

When you make a MongoDB insert request that changes an object so it becomes larger than the maximum allowed size (16mb) you will get the following error:

MongoError: BSONObj size: 20971588 (0x1400044) is invalid. Size must be between 0 and 16793600(16MB) First element: _id: ObjectId('60a277f50a5f8b012a761672')    at Function.create ...    {  driver: true,  index: 0,  code: 10334}    

When you do a MongoDB query that is too large (like in the question). You will be presented with the following error:

RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range. It must be >= 0 && <= 17825792. Received 17825794    at validateOffset (buffer.js:104:3)    at Buffer.write (buffer.js:1055:5)    ... {  code: 'ERR_OUT_OF_RANGE'}

This is important because if you do for example an upsert that inserts or updates 10mb of data your query surpasses the max ~17mb of a query. (not entirely sure this was intended by the developers)

Lastly when you do an insert that does not exceed the maximum query size, but does exceed the maximum BSON document size you will be presented with this error:

MongoError: object to insert too large. size in bytes: 16777272, max size: 16777216    at Function.create ...     {  driver: true,  index: 0,  code: 2}

This was tested with:

  • Node v14.16.0
  • MongoDB node module v3.6.4

I hope this helps anyone who landed on this question.