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

Filter is mutated in place by Query modifiers #14610

Closed
2 tasks done
sclamage opened this issue May 23, 2024 · 3 comments
Closed
2 tasks done

Filter is mutated in place by Query modifiers #14610

sclamage opened this issue May 23, 2024 · 3 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@sclamage
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.4.0

Node.js version

20.13.1

MongoDB server version

7.0.9

Typescript version (if applicable)

No response

Description

I'm experiencing more or less the same bug reported in #14580 (and also #14567). This was supposedly fixed in the 8.3.5 release, but the below test case fails in every version I tested from 7.3.5 through 8.4.0.

Steps to Reproduce

Here's the test code:


bug().then(() => {});

async function bug() {
    await mongoose.connect('mongodb://127.0.0.1:27017/test');

    const personSchema = new mongoose.Schema({
        name: String,
        age: Number
    });
    const Person = mongoose.model('Person', personSchema);

    const query = [{name: 'Me', age: '20'}, {name: 'You', age: '50'}]
    console.log(query)

    const persons = await Person.find({$or: query})
    console.log(query)

    await mongoose.disconnect()
}

Expected Behavior

I acknowledge that the above code doesn't really look correct - age is a Number, so a query of {age: '50'} doesn't make sense and shouldn't return any results. However, I would definitely not expect find() to modify the query arguments!

When I run this against mongoose 7.3.4 (or earlier) the test code outputs the same query object twice, as expected:

[ { name: 'Me', age: '20' }, { name: 'You', age: '50' } ]
[ { name: 'Me', age: '20' }, { name: 'You', age: '50' } ]

However, if I run this against mongoose 7.4.0 or later, it modifies the query argument in-place, and outputs:

[ { name: 'Me', age: '20' }, { name: 'You', age: '50' } ]
[ { name: 'Me', age: 20 }, { name: 'You', age: 50 } ]
@vkarpov15 vkarpov15 added this to the 8.4.1 milestone May 23, 2024
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label May 23, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.4.1, 7.6.13 May 24, 2024
vkarpov15 added a commit that referenced this issue May 24, 2024
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels May 24, 2024
vkarpov15 added a commit that referenced this issue May 30, 2024
fix(query): shallow clone $or and $and array elements to avoid mutating query filter arguments
@sclamage
Copy link
Author

Thanks for fixing this!

I can see it's fixed in 7.6.13, but it's still broken in 8.4.1 and all 8.x releases that I tested with. Is it possible to include this fix in newer releases too? Thank you!

@vkarpov15
Copy link
Collaborator

@sclamage try 8.4.2 or 8.5.5? This fix should be in those releases.

@sclamage
Copy link
Author

Thanks! I can confirm my test case succeeds in 8.5.5, but not 8.4.2.

8.5.5 is good enough for me though - thank you!

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

2 participants