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

Using concat with an empty array on an array of subdocuments seems to incorrectly mark fields as changed #10272

Closed
m-weeks opened this issue May 19, 2021 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@m-weeks
Copy link

m-weeks commented May 19, 2021

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

What is the current behavior?
Using concat on an already changed array seems to override the fact that the array has changed. This seems to be a very obscure issue and I have only been able to reproduce it by calling concat with an empty array.

If the current behavior is a bug, please provide the steps to reproduce.
Using the following model:

const MyChildSchema = new Schema({
  name: {
    type: "String"
  }
});

const MyModelSchema = new Schema({
  children: [MyChildSchema]
});

const MyModel = model("MyModel", MyModelSchema);

Doing the following operation:

var myModel = await new MyModel({
  children: [{ name: "John" }, { name: "Jane" }]
}).save();

var myModel = await MyModel.findById(newMyModel._id);

myModel.children = [{ name: "John" }];

myModel.children = myModel.children.concat([]); // In my case, this empty array comes from a function

myModel.children.push({ name: 'Mary' });

await myModel.save();

myModel = await MyModel.findById(newMyModel._id);
console.log(myModel.children)

The console.log should contain the names "John" and "Mary", but contains "John", "Jane", and "Mary". The reason is because mongoose performs only a $push query for "Mary"

What is the expected behavior?

The children on the document should only contain the names "John" and "Mary"

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

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label May 21, 2021
@IslandRhythms
Copy link
Collaborator

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

const MyChildSchema = new Schema({
    name: {
      type: "String"
    }
  });
  
  const MyModelSchema = new Schema({
    children: [MyChildSchema]
  });
  
  const MyModel = mongoose.model("MyModel", MyModelSchema);

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

  const myModel = await new MyModel({
    children: [{ name: "John" }, { name: "Jane" }]
  }).save();
  
  const search = await MyModel.findById({_id: myModel._id});
  
  search.children = [{ name: "John" }];
  
  search.children = search.children.concat([]); // In my case, this empty array comes from a function
  
  search.children.push({ name: 'Mary' });
  
  await search.save();
  
  const entry = await MyModel.findById({_id: myModel._id});
  console.log(entry.children)
}

test();

@vkarpov15 vkarpov15 modified the milestones: 5.12.11, 5.12.12 May 21, 2021
vkarpov15 added a commit that referenced this issue May 27, 2021
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