Automattic/mongoose · error · MongooseError

documents.${i} was not a mongoose document, documents must b

Error message

documents.${i} was not a mongoose document, documents must be an array of mongoose documents (instanceof mongoose.Document).

What it means

Error "documents.${i} was not a mongoose document, documents must be an array of mongoose documents (instanceof mongoose.Document)." thrown in Automattic/mongoose.

Source

Thrown at lib/model.js:3934

 * @param {Document[]} documents The array of documents to build write operations of
 * @param {object} options
 * @param {boolean} options.skipValidation defaults to `false`, when set to true, building the write operations will bypass validating the documents.
 * @param {boolean} options.timestamps defaults to `null`, when set to false, mongoose will not add/update timestamps to the documents.
 * @return {Promise[]} Returns a array of all Promises the function executes to be awaited.
 * @api private
 */

Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, options) {
  if (!Array.isArray(documents)) {
    throw new MongooseError(`bulkSave expects an array of documents to be passed, received \`${documents}\` instead`);
  }

  setDefaultOptions();

  const writeOperations = documents.map((document, i) => {
    if (!options.skipValidation) {
      if (!(document instanceof Document)) {
        throw new MongooseError(`documents.${i} was not a mongoose document, documents must be an array of mongoose documents (instanceof mongoose.Document).`);
      }
      if (options.validateBeforeSave == null || options.validateBeforeSave) {
        const err = document.$__validateSync();
        if (err != null) {
          throw err;
        }
      }
    }

    const isANewDocument = document.isNew;
    if (isANewDocument) {
      // Like `$__version()` on insert, initialize the version key so
      // inserted documents get the same shape as `save()` gives them
      const versionKey = document.$__schema.options.versionKey;
      if (versionKey) {
        document.$__setValue(versionKey, 0);
      }
      const writeOperation = { insertOne: { document } };

View on GitHub (pinned to 49cdab0136)

When it happens

Trigger: Thrown at lib/model.js:3934 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21). Data as JSON: /api/errors/948b686b64a8150b. Report an issue: GitHub.