October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

MongoDB’s 16 MiB Document Limit: Find the Document That Breaks Your Agent Run

A compact $bsonSize aggregation identifies the largest stored MongoDB documents and helps separate an oversized document from a cursor-batch or Search change-stream issue.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If a MongoDB-backed agent run fails while saving or returning data, rank the documents in the affected collection by their encoded BSON size. MongoDB caps each BSON document at 16 mebibytes (MiB); a smaller cursor batch does not change that per-document limit. In mongosh, use $bsonSize on $$ROOT and return only each document’s _id and byte count to identify the largest stored records.

Find the largest stored documents

Run this aggregation against the collection involved in the failed operation, replacing collection with its name:

As an Amazon Associate I earn from qualifying purchases.

db.collection.aggregate([
  {
    $project: {
      _id: 1,
      bsonBytes: { $bsonSize: "$$ROOT" }
    }
  },
  { $sort: { bsonBytes: -1 } },
  { $limit: 20 }
])

$bsonSize returns the size in bytes of an object encoded as BSON. Here, $$ROOT means the current document; the pipeline projects a compact row containing its identifier and size, sorts largest first, and limits the output to 20. See MongoDB’s $bsonSize documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use the relevant filter as well if you can narrow the scan to the records involved in the failing agent operation. The results can tell you which stored documents are largest, but not which one caused a particular failure without matching them to the operation and error details.

If the failing document is not in the collection

A collection scan cannot find a document that was rejected before insertion or whose failed update was never applied. In that case, capture the pending payload at the application’s write path and measure the object being sent. The aggregation ranks only documents currently present.

What the 16 MiB limit applies to

MongoDB’s Database Manual states: “The maximum BSON document size is 16 mebibytes.” The limit applies to the encoded document as a whole, including embedded objects and arrays. A large nested value or growing embedded list can therefore push the containing document over the cap even if no single field appears unusually large.

The aggregation itself must also obey the per-result-document limit. Keep diagnostic output compact: projecting an identifier and byte count avoids building a large result document while measuring the originals. See MongoDB’s aggregation pipeline limits.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Separate a document-size error from a cursor-batch limit

MongoDB also limits a cursor batch to at most 16 MiB in total, but that is a separate constraint from the maximum size of one BSON document. A batch contains documents; each document is independently subject to the 16 MiB cap. Reducing batch size may affect how many results arrive together, but it cannot make an oversized document valid. MongoDB describes the distinction in its limits documentation and cursor documentation.

Check whether the failure is in MongoDB Search replication

If ordinary reads and writes work but Search indexing stalls or replication lag grows, the oversized object may be a change-stream event rather than a stored document. A change event includes metadata as well as document data, so an update to an already-large document can produce an event that exceeds the BSON limit.

For self-managed MongoDB Search, inspect mongot logs and the documented metrics. MongoDB identifies messages such as change-stream payload exceeding 16MB BSON limit, BSONObjectTooLarge, Executor error during getMore, and code 10334 as relevant clues. Its Search troubleshooting guidance discusses reducing document size, avoiding large updates where possible, replacing a document rather than applying a large update where appropriate, and reducing update-event metadata. This diagnosis applies when symptoms point to Search indexing; it is not a general explanation for every failed agent run.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose a repair that fits the data

Once you identify the oversized record—or establish that the rejected payload never reached the collection—change the data shape or storage location that is driving its size. MongoDB’s guidance on unbounded arrays and its data modeling documentation describe ways to structure related data.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Growing embedded arrays: If a document accumulates an unbounded list, consider splitting the list into smaller documents and referencing related records. Patterns such as subset or outlier designs may help when they suit how the application reads the data.
  • Images and bulky assets: When practical, keep large images or other assets outside the database deployment and store references in MongoDB. Including bulky assets in documents makes the document cap easier to reach.
  • Content larger than one document: MongoDB points to GridFS for content that must exceed the BSON document maximum. See the GridFS documentation.
  • Frequently accessed data: If the application usually fetches fields together, keep that access pattern in view when deciding what to embed or separate. Referencing data can reduce document size, but it changes how the application retrieves related information.

If the size scan does not explain the incident, preserve the failing operation and full error details; the exact cause cannot be determined from the limit alone. Confirm whether the failure occurred on insert, update, read, or Search indexing before choosing a repair.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.