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 throws an error when set timestamps: false in schema #10048

Closed
SoftwareSing opened this issue Mar 20, 2021 · 2 comments · Fixed by #10050
Closed

bulkWrite throws an error when set timestamps: false in schema #10048

SoftwareSing opened this issue Mar 20, 2021 · 2 comments · Fixed by #10050
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

Comments

@SoftwareSing
Copy link
Contributor

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

What is the current behavior?

When set timestamps: false in schema, bulkWrite with insertOne or replaceOne will throw an error.
But it works fine when set timestamps: null or just don't set timestamps.

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

const mongoose = require('mongoose')

module.exports = async function bulkInsertOne () {
  const schema = new mongoose.Schema({ name: String }, { timestamps: false })
  const Model = mongoose.model('MyModel', schema)
  await Model.bulkWrite([{
    insertOne: {
      document: { name: 'test' }
    }
  }])
}

error:

(node:24085) UnhandledPromiseRejectionWarning: TypeError: doc.initializeTimestamps is not a function
    at /home/miku/_____/code/test-for-mongoose-timestamps/node_modules/mongoose/lib/helpers/model/castBulkWrite.js:24:13
    at /home/miku/_____/code/test-for-mongoose-timestamps/node_modules/mongoose/lib/model.js:3500:35
    at each (/home/miku/_____/code/test-for-mongoose-timestamps/node_modules/mongoose/lib/helpers/each.js:11:5)
    at /home/miku/_____/code/test-for-mongoose-timestamps/node_modules/mongoose/lib/model.js:3500:5
    at /home/miku/_____/code/test-for-mongoose-timestamps/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/home/miku/_____/code/test-for-mongoose-timestamps/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
    at Mongoose._promiseOrCallback (/home/miku/_____/code/test-for-mongoose-timestamps/node_modules/mongoose/lib/index.js:1149:10)
    at Function.Model.bulkWrite (/home/miku/_____/code/test-for-mongoose-timestamps/node_modules/mongoose/lib/model.js:3498:23)
    at bulkInsertOne (/home/miku/_____/code/test-for-mongoose-timestamps/src/bulkInsertOne.js:6:15)
    at main (/home/miku/_____/code/test-for-mongoose-timestamps/src/main.js:7:9)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

What is the expected behavior?

It should not throw any error.

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

  • Node.js version: 14.16.0
  • Mongoose version: 5.12.1
  • MongoDB version: 4.2.13
SoftwareSing added a commit to SoftwareSing/mongoose that referenced this issue Mar 20, 2021
SoftwareSing added a commit to SoftwareSing/mongoose that referenced this issue Mar 20, 2021
@IslandRhythms IslandRhythms linked a pull request Mar 22, 2021 that will close this issue
@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Mar 22, 2021
@vkarpov15 vkarpov15 added this to the 5.12.3 milestone Mar 22, 2021
@vkarpov15 vkarpov15 reopened this Mar 23, 2021
@vkarpov15
Copy link
Collaborator

@SoftwareSing the below script executes without error. Please modify the below script to demonstrate your issue:

const mongoose = require('mongoose');

mongoose.set('debug', true);

run().catch(err => console.log(err));

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });

  await mongoose.connection.dropDatabase();

  const schema = new mongoose.Schema({ name: String }, { timestamps: false });
  const Model = mongoose.model('MyModel', schema)
  await Model.bulkWrite([{
    insertOne: {
      document: { name: 'test' }
    }
  }]);

  console.log('Done');
}

@vkarpov15 vkarpov15 removed this from the 5.12.3 milestone Mar 30, 2021
@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Mar 30, 2021
@SoftwareSing
Copy link
Contributor Author

@vkarpov15 well...... it's already fixed by #10050 , I don't know why this issue is reopened.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants