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

.cursor() executes pre-find hooks but doesn't change the query and has a different behavior from .find() queries that don't stream #13453

Closed
2 tasks done
ShriprajwalK opened this issue Jun 1, 2023 · 0 comments
Milestone

Comments

@ShriprajwalK
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

6.10.5

Node.js version

14.x

MongoDB server version

4.4

Typescript version (if applicable)

No response

Description

The behavior of pre-find hooks is different when we do a .find() query without streaming vs with streaming. The behavior without streaming seems correct. This has been fixed in version 7.2.1 but seems to still exist in version 6.

Steps to Reproduce

const mongoose = require("mongoose");
 mongoose.set("debug", true);
 
 const schema = new mongoose.Schema({
     name: String,
     age: Number,
 });
 
 schema.pre("find", function (next) {
     console.log("Pre-find hook on Model executed");
     this._fields = { age: 1 };
     console.log(this._fields);
     return next();
 });
 
 const Model = mongoose.model("Model", schema);
 
 main().catch((err) => console.log(err));
 
 async function main() {
     await mongoose.connect("mongodb://[127.0.0.1:27017/vymo-lms](http://127.0.0.1:27017/test-db)");
     await Model.find({}).select("name").exec();
 }

The query produced is:

Mongoose: models.find({}, { projection: { age: 1 } })

If we instead do:

     await Model.find({}).select("name").cursor();

The query produced in this case though is:

Mongoose: models.find({}, { projection: { name: 1 } })

Ideally, there should be consistent behavior between both, the cursor behavior seems to be unexpected.

This seems to have been fixed in version 7.2.1(versions below this still seem to have this behavior) in:
fix(cursor): handle calling skipMiddlewareFunction() in pre('find') middleware with cursors #13436 #13411
Is it possible to fix this in version 6 as well?

Expected Behavior

No response

@vkarpov15 vkarpov15 added this to the 6.11.2 milestone Jun 2, 2023
vkarpov15 added a commit that referenced this issue Jun 7, 2023
fix(cursor): allow find middleware to modify query cursor options
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