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

Mongoose plugin does not cast the query before execution #312

Open
SeanLMcCullough opened this issue Nov 25, 2021 · 3 comments
Open

Mongoose plugin does not cast the query before execution #312

SeanLMcCullough opened this issue Nov 25, 2021 · 3 comments

Comments

@SeanLMcCullough
Copy link

SeanLMcCullough commented Nov 25, 2021

I came across this issue today when querying a Mongoose schema with a ref and ObjectId to another collection.
Usually, Mongoose will cast the query before execution, and convert all ObjectId properties to ObjectIds before executing the query. When using the mongoose plugin, this does not happen, so fields that should be ObjectIds are instead queried as strings. The outcome is that no documents will be found.

Example:

const Model = new mongoose.Schema({
  otherId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'OtherModel',
    index: true
  }
})

const options = {
  query: {
    { otherId: '619f1683a899a80029b18989' }
  }
}

const result = await Model.paginate(options)
console.log(result.results) // Length is 0 because otherId is handled as a string, not ObjectId.

The mongoose plugin should ideally cast the query before executing the find method. For example, the following casting works before executing the query:

options.query = new mongoose.Query().cast(Model, options.query)

Having the mongoose plugin handle this internally would likely be a good enhancement, so all of the Mongoose special sauce is handled ahead of the query on the collection.

@bradvogel
Copy link
Contributor

@vkarpov15 might know

@dmarinere
Copy link

dmarinere commented Oct 17, 2022

Please help with this @bradvogel @jsalvata

@bradvogel
Copy link
Contributor

@dmarinere I'm sorry, I don't know Mongoose well. I haven't used it.

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

3 participants