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

Chaining .find with $and condition overwrites prior conditions #12944

Closed
2 tasks done
Rouby opened this issue Jan 25, 2023 · 0 comments
Closed
2 tasks done

Chaining .find with $and condition overwrites prior conditions #12944

Rouby opened this issue Jan 25, 2023 · 0 comments
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Milestone

Comments

@Rouby
Copy link

Rouby commented Jan 25, 2023

Prerequisites

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

Mongoose version

6.7.0

Node.js version

18.x

MongoDB server version

4.x

Typescript version (if applicable)

No response

Description

Chaining multiple .find (or other methods that manipulate the conditions) with $and (or other array-based conditions) results in the latest method silently overwriting any previous conditions.

This issue is relevant specifically for the use-case with the package @casl/mongoose that has utility methods to add constraints to queries based on the ability of an user.

E.g.

    MyModel.accessibleBy(userAbility)
      .countDocuments(additionalConditions)
      .exec(),

The query-conditions generated by accessibleBy might be silently overwritten if they happen to utilize $and and the additionalConditions do so too.

Steps to Reproduce

MyModel.find({ $and: [{ type: { $in: ['X'] } }] })
        .find({ $and: [{ type: { $in: ['Y'] } }] })
        .countDocuments({ $and: [{ type: { $in: ['Z'] } }] })
        .getQuery(),

//? {"$and":[{"type":{"$in":["Z"]}}]}

Expected Behavior

Since other conditions are merged, I would expect array-conditions to also be merged.

MyModel.find({ $and: [{ type: { $in: ['X'] } }] })
        .find({ $and: [{ type: { $in: ['Y'] } }] })
        .countDocuments({ $and: [{ type: { $in: ['Z'] } }] })
        .getQuery(),

//? {"$and":[{"type":{"$in":["X"]}},{"type":{"$in":["Y"]}},{"type":{"$in":["Z"]}}]}
@IslandRhythms IslandRhythms added the new feature This change adds new functionality, like a new method or class label Jan 25, 2023
@vkarpov15 vkarpov15 added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature and removed new feature This change adds new functionality, like a new method or class labels Feb 3, 2023
@vkarpov15 vkarpov15 modified the milestones: 6.9.1, 7.0 Feb 3, 2023
vkarpov15 added a commit that referenced this issue Feb 18, 2023
BREAKING CHANGE: merge `$and`, `$or` conditions in query filters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

No branches or pull requests

3 participants