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

Query middleware not executed when using cursor #5096

Closed
zoellner opened this issue Mar 22, 2017 · 3 comments
Closed

Query middleware not executed when using cursor #5096

zoellner opened this issue Mar 22, 2017 · 3 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@zoellner
Copy link
Contributor

Not sure if it is a bug or by design, but query middleware is ignored for .cursor() calls. If it's by design it should be mentioned in the docs.

Expected behavior would be the same query for either call.
Current behavior is (with test script below):
find(): Mongoose: tests.find({ b: true }, { fields: {} })
find().cursor(): Mongoose: tests.find({ isB: true }, { retainKeyOrder: false, fields: {} })

'use strict';

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/github-mongoose-query-middleware-cursor', {});
mongoose.set('debug', true);

const TestSchema = new mongoose.Schema({a: {type: String}, b: {type: Boolean}});

const queryMiddleware = function() {
  if (this._conditions.hasOwnProperty('isB')) {
    this._conditions.b = this._conditions.isB;
    delete this._conditions.isB;
  }
};

TestSchema.pre('find', queryMiddleware);
TestSchema.pre('findOne', queryMiddleware);
TestSchema.pre('count', queryMiddleware);

const Test = mongoose.model('Test', TestSchema);

const query = { user: { $type: 'string' } };

Test.insertMany([{a: 'b true', b: true}, {a: 'b false', b: false}], function (err) {
  if (err) {console.error(err);}
  Test.find({isB: true}).exec(function(err, t) {
    if (err) {console.error(err);}
    console.log(t);

    let cursor = Test.find({isB: true}).cursor();
    process.exit();
  });
});

using
node.js v6.9.5
mongoose v4.9.1
mongodb v3.4.1

@sobafuchs
Copy link
Contributor

@zoellner thanks for the clear repro script, this looks like a bug

@sobafuchs sobafuchs added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Mar 24, 2017
@sobafuchs sobafuchs added this to the 4.9.2 milestone Mar 24, 2017
vkarpov15 added a commit that referenced this issue Mar 25, 2017
@joniba
Copy link

joniba commented Jun 28, 2018

is there a workaround for those of us that cannot upgrade right now to a version with the fix?

@vkarpov15
Copy link
Collaborator

@joniba not currently. What version are you on and what prevents you from upgrading to 4.9.2?

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

4 participants