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

bulkSave() throws an error when an unchanged document is passed #10437

Closed
lucashpmelo opened this issue Jul 8, 2021 · 0 comments
Closed

bulkSave() throws an error when an unchanged document is passed #10437

lucashpmelo opened this issue Jul 8, 2021 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@lucashpmelo
Copy link

lucashpmelo commented Jul 8, 2021

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

A bug

What is the current behavior?

When an unmodified object is passed to bulkSave() an error is raised:
TypeError: Cannot read property '0' of undefined

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

const mongoose = require('mongoose')
const { Schema } = mongoose

const testSchema = new Schema({
  name: {
    type: String,
    required: true,
  },
  date: {
    type: Date,
    required: true,
  },
  value: {
    type: Number,
    required: true,
    default: 0,
  },
})

new mongoose.model('Test', testSchema)
const Test = mongoose.model('Test')

async function bulkSave(data) {
  const bulk = []

  for (const i in data) {
    const { name, date, value } = data[i]

    let test = await Test.findOne({ name: name, date: date })

    if (!test) {
      test = new Test(data[i])
    }

    test.value = value

    bulk.push(test)
  }

  return await Test.bulkSave(bulk)
}

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

  await mongoose.connection.dropDatabase()

  const data1 = [
    { name: 'Test 1', date: '2021-08-01T00:00:00.000Z', value: 10 },
    { name: 'Test 1', date: '2021-08-02T00:00:00.000Z', value: 20 },
    { name: 'Test 2', date: '2021-08-01T00:00:00.000Z', value: 15 },
  ]

  await bulkSave(data1)

  const data2 = [
    { name: 'Test 1', date: '2021-08-01T00:00:00.000Z', value: 15 },
    { name: 'Test 1', date: '2021-08-02T00:00:00.000Z', value: 20 }, // <-- ERROR
    { name: 'Test 2', date: '2021-08-02T00:00:00.000Z', value: 25 },
  ]

  await bulkSave(data2)

  console.log('Done')
}

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

What is the expected behavior?

bulkSave() handle whether the document has changes or not.

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

Node v13.11.0
Mongoose v5.13.2
MongoDB 4.2.5

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jul 9, 2021
@vkarpov15 vkarpov15 added this to the 5.13.3 milestone Jul 11, 2021
vkarpov15 added a commit that referenced this issue Jul 28, 2021
test(model): assert Model.bulkSave(...) works if some document is not modified
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

3 participants