typegoose: updateMany() returns wrong nModified value

Versions

  • NodeJS: 12.18.3
  • Typegoose(NPM): 7.4.1
  • mongoose: 5.10.7

What is the Problem?

updateMany().nModified value = the number of documents that are matched rather than changed

Code Example

import { getModelForClass, prop } from '@typegoose/typegoose'

class Vendor {
  @prop({ required: true, index: true })
  name: string
}

const VendorModel = getModelForClass(Vendor)

async function start() {
  await VendorModel.create({ name: 'TEST-1' })
  await VendorModel.create({ name: 'TEST-1' })
  await VendorModel.create({ name: 'TEST-2' })

  const result = await VendorModel.updateMany({}, { name: 'TEST-2' })

  console.log(result.n) // 3 (correct value)
  console.log(result.nModified) // 3 (wrong value, should be 2)
}

start()

Update: this example is incorrect. Check this: https://github.com/typegoose/typegoose/issues/388#issuecomment-699531376

Do you know why it happens?

No, but maybe there is a mongoose problem?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 30 (17 by maintainers)

Most upvoted comments

@vovarevenko - Um, but you aren’t filtering on anything. So, all three documents are being overwritten, even the last doc with the same object/ value. Your expectation that the last doc not to be overwritten is incorrect. At least, that seems to be the situation. 😁

Scott