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

doc.isModified() wrong when _id: false and updated this way #11172

Closed
johnpeb opened this issue Jan 3, 2022 · 0 comments
Closed

doc.isModified() wrong when _id: false and updated this way #11172

johnpeb opened this issue Jan 3, 2022 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@johnpeb
Copy link
Contributor

johnpeb commented Jan 3, 2022

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

What is the current behavior?

doc.isModified() is incorrectly returning false when a subdocument is modified under an array with _id disabled and when the array is completely assigned to a value that is identical, but then later an array member is changed.

See REQUIREMENT 1 and REQUIREMENT 2 below.

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

Run the script below: npx ts-node scripts/mongoose.ts

Script.ts:

import { Schema } from 'mongoose';
import * as mongoose from 'mongoose'

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

async function run(): Promise<void> {
  await mongoose.connect('mongodb://localhost:27017/test', {
  });

  await mongoose.connection.dropDatabase();

  const Order = mongoose.model('Order', new Schema({
    cumulativeConsumption: [{
      _id: false, // REQUIREMENT 1 <<<<<
      unit: String,
      value: Number
    }]
  }));

  await Order.create({
    cumulativeConsumption: [{ unit: 'foo', value: 123 }, { unit: 'bar', value: 42 }]
  });

  const doc = await Order.findOne();
  doc.cumulativeConsumption = doc.toObject().cumulativeConsumption // REQUIREMENT 2 <<<<<
  // console.log(doc.modifiedPaths())
  const match = doc.cumulativeConsumption.find(o => o.unit === 'bar');
  match.value = 43;
  match.unit = 'baz'
  console.log(`Modified: ${doc.isModified()}, expecting true`)

  process.exit()
}

What is the expected behavior?

  1. modifiedPaths() should reflect the change after the match.value = 43; line
  2. isModified() should be true after the match.value = 43; line

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

Mongoose 6.1.4, node v12.22.7, mongo v4.2.8

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jan 5, 2022
@vkarpov15 vkarpov15 added this to the 6.1.7 milestone Jan 5, 2022
AbdelrahmanHafez added a commit to AbdelrahmanHafez/mongoose that referenced this issue Jan 8, 2022
AbdelrahmanHafez added a commit to AbdelrahmanHafez/mongoose that referenced this issue Jan 8, 2022
vkarpov15 added a commit that referenced this issue Jan 15, 2022
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