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

CoreMongooseArray#includes different behaviors in different versions #9578

Closed
koevas1226 opened this issue Nov 24, 2020 · 1 comment
Closed

Comments

@koevas1226
Copy link

koevas1226 commented Nov 24, 2020

Do you want to request a feature or report a bug?
bug
What is the current behavior?
includes() different behaviors
If the current behavior is a bug, please provide the steps to reproduce.

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true, useUnifiedTopology: true });
// model
const userModel = mongoose.model('User', {
  name: String,
  friends: [{ type: mongoose.SchemaTypes.ObjectId, ref: 'User' }]
});
// init function
async function init(){
  const user1 = new userModel({ name: 'user1' });
  const user2 = new userModel({ name: 'user2' });
  const user1Model = await user1.save();
  const user2Model = await user2.save();
  const user3 = new userModel({
    name: 'user3',
    friends: [user1Model._id, user2Model._id]
  });
  const user3Model = await user3.save();
}
// code
await init();
const user1 = await userModel.findOne({ name: 'user1' }).exec();
const user2 = await userModel.findOne({ name: 'user2' }).exec();
const user3 = await userModel.findOne({ name: 'user3' }).populate('friends', '_id').exec();
const ids = user3.friends.map(u=> u._id);
// behavior
// version 5.7.1 
ids.includes(user1._id); // true
ids.includes(user2._id); // true
// version 5.5.15
ids.includes(user1._id); // false
ids.includes(user2._id); // false

What is the expected behavior?
it should be the same behavior.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
nodejs v10.22.0
mongoose v5.5.15 && v5.7.1
mongodb v3.4.6

I had checked the release log but didn't found any breaking changes.
Since https://github.com/Automattic/mongoose/blob/master/History.md#554--2019-04-25 v5.5.4 already supported #includes(), so why it has different behaviors?

@vkarpov15 vkarpov15 added this to the 5.10.18 milestone Nov 28, 2020
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Nov 28, 2020
@vkarpov15 vkarpov15 modified the milestones: 5.10.18, 5.10.19 Nov 29, 2020
@vkarpov15
Copy link
Collaborator

I took a closer look and this is an unintended consequence of #7798 in v5.6.0: as of v5.6.0, user3.friends.map() returns a Mongoose array rather than a vanilla array, and Mongoose arrays support searching for documents by their _id. We intend to change this behavior in v6.0, see #8356. But changing it in a minor release seems a bit too heavy.

As a workaround, you can do const ids = [].concat(user3.friends.map(u=> u._id)); to ensure that you get a vanilla JavaScript array.

@vkarpov15 vkarpov15 modified the milestones: 5.10.19, 6.0 Nov 29, 2020
@vkarpov15 vkarpov15 added backwards-breaking and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Nov 29, 2020
@vkarpov15 vkarpov15 removed this from the 6.0 milestone Jul 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants