Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BulkWrite's updates seem to mess with schema type #9350

Closed
Marsup opened this issue Aug 21, 2020 · 2 comments
Closed

BulkWrite's updates seem to mess with schema type #9350

Marsup opened this issue Aug 21, 2020 · 2 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Marsup
Copy link

Marsup commented Aug 21, 2020

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

Crashing with an error within an error. That is:

  errors: {
    foo: CastError: Cast to Boolean failed for value "undefined" at path "foo"
        at model.$set (./mongoose-bug/node_modules/mongoose/lib/document.js:1274:9)
        at model._handleIndex (./mongoose-bug/node_modules/mongoose/lib/document.js:996:14)
        at model.$set (./mongoose-bug/node_modules/mongoose/lib/document.js:937:22)
        at model.Document (./mongoose-bug/node_modules/mongoose/lib/document.js:146:12)
        at model.Model (./mongoose-bug/node_modules/mongoose/lib/model.js:106:12)
        at new model (./mongoose-bug/node_modules/mongoose/lib/model.js:4675:15)
        at ./mongoose-bug/node_modules/mongoose/lib/model.js:3041:22
        at ./mongoose-bug/node_modules/mongoose/lib/model.js:3077:7
        at Array.forEach (<anonymous>)
        at ./mongoose-bug/node_modules/mongoose/lib/model.js:3076:15
        at ./mongoose-bug/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
        at new Promise (<anonymous>)
        at promiseOrCallback (./mongoose-bug/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
        at Function.create (./mongoose-bug/node_modules/mongoose/lib/model.js:3011:10)
        at ./mongoose-bug/test.js:17:15
        at processTicksAndRejections (internal/process/task_queues.js:93:5) {
      stringValue: '"undefined"',
      messageFormat: undefined,
      kind: 'Boolean',
      value: undefined,
      path: 'foo',
      reason: TypeError: Cannot read property 'omitUndefined' of undefined
          at SchemaBoolean._castNullish (./mongoose-bug/node_modules/mongoose/lib/schema/boolean.js:233:93)
          at SchemaBoolean.SchemaType.applySetters (./mongoose-bug/node_modules/mongoose/lib/schematype.js:1066:17)
          at model.$set (./mongoose-bug/node_modules/mongoose/lib/document.js:1227:20)
          at model._handleIndex (./mongoose-bug/node_modules/mongoose/lib/document.js:996:14)
          at model.$set (./mongoose-bug/node_modules/mongoose/lib/document.js:937:22)
          at model.Document (./mongoose-bug/node_modules/mongoose/lib/document.js:146:12)
          at model.Model (./mongoose-bug/node_modules/mongoose/lib/model.js:106:12)
          at new model (./mongoose-bug/node_modules/mongoose/lib/model.js:4675:15)
          at ./mongoose-bug/node_modules/mongoose/lib/model.js:3041:22
          at ./mongoose-bug/node_modules/mongoose/lib/model.js:3077:7
          at Array.forEach (<anonymous>)
          at ./mongoose-bug/node_modules/mongoose/lib/model.js:3076:15
          at ./mongoose-bug/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
          at new Promise (<anonymous>)
          at promiseOrCallback (./mongoose-bug/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
          at Function.create (./mongoose-bug/node_modules/mongoose/lib/model.js:3011:10)
    }
  },

If the current behavior is a bug, please provide the steps to reproduce.

Here is a small snippet demonstrating the issue:

const mongoose = require('mongoose');

const Schema = new mongoose.Schema({
  foo: Boolean,
});

const Model = mongoose.model('test', Schema);

(async function () {
  await mongoose.connect('mongodb://localhost:27017');

  await Model.bulkWrite([
    { insertOne: { document: { foo: undefined } } },
    { updateOne: { filter: {}, update: { $set: { foo: true } } } },
  ]);

  await Model.create({
    foo: undefined,
  });

  await mongoose.disconnect();
})().then(console.info.bind(console, 'OK'), console.error.bind(console, 'NOK'));

Simply commenting the updateOne line seems to make the issue go away. updateMany seems to have the same behavior.

What is the expected behavior?

No error.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node.js 14.8.0, Mongoose 5.10.0, MongoDB 4.2.8

@AbdelrahmanHafez AbdelrahmanHafez added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Aug 21, 2020
@vkarpov15 vkarpov15 added this to the 5.10.1 milestone Aug 22, 2020
vkarpov15 added a commit that referenced this issue Aug 23, 2020
@eric-swann-q2
Copy link

This is currently a showstopper for us on 5.10...none of our bulk updates are working. Is there a workaround for this issue until the fix is out? Thanks!

@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Aug 26, 2020
@vkarpov15
Copy link
Collaborator

@eric-swann-q2 we're releasing 5.10.1 now, sorry for the trouble!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

4 participants