Skip to content

BulkWrite updateOne with filter $elemMatch { $elemMatch: { $in: [] } } throwing TypeError #14678

Closed

Description

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.4.3

Node.js version

20.14.0

MongoDB server version

4.4.13

Typescript version (if applicable)

No response

Description

After upgrading mongoose 7.3.4 -> 7.6.13, we started to experience some TypeErrors with one of our bulkWrite operations. We also tried upgrading to 8.4.3, but we still have been facing the same issues.

Error stack:

TypeError: Cannot read properties of undefined (reading 'schema')
    at SchemaArray.cast$elemMatch (PATH\node_modules\mongoose\lib\schema\array.js:626:59)
    at SchemaArray.castForQuery (PATH\node_modules\mongoose\lib\schema\array.js:572:20)
    at cast (PATH\node_modules\mongoose\lib\cast.js:324:43)
    at PATH\node_modules\mongoose\lib\helpers\model\castBulkWrite.js:100:37
    at PATH\node_modules\mongoose\lib\model.js:3584:37
    at each (PATH\node_modules\mongoose\lib\helpers\each.js:11:5)
    at PATH\node_modules\mongoose\lib\model.js:3584:7
    at new Promise (<anonymous>)
    at Function.bulkWrite (PATH\node_modules\mongoose\lib\model.js:3583:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Steps to Reproduce

Here is a simple script that replicates the error-producing behavior on our backend

const mongoose = require('mongoose');
require('dotenv').config();

(async () => {
  await mongoose.connect(`mongodb://${process.env.HOST}:${process.env.PORT}/${process.env.DB_NAME}`);

  const schema = new mongoose.Schema({
    name: String,
    ids: [String] // problematic type
  });

  const Model = mongoose.model('Test', schema);

  await Model.deleteMany();

  try {
    await Model.bulkWrite([
      {
        updateOne: {
          filter: {
            ids: {
              $elemMatch: {
                $in: ['1']
              }
            }
          },
          update: {
            $set: {
              name: 'test'
            },
            $addToSet: {
              ids: {
                $each: ['1', '2']
              }
            }
          },
          upsert: true
        }
      }
    ], { ordered: true, j: true });
  } catch (error) {
    console.error(error);
  }

  await mongoose.disconnect();
})();

Expected Behavior

CASE 1: Filter-matching document found
EXPECTED: ids that don't already exist in document are added to the array ids.

CASE 2: Filter-matching document not found
EXPECTED: A new document is created

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions