Skip to content

Applying sort to a count query fails #3914

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

Closed
sabrehagen opened this issue Feb 25, 2016 · 6 comments
Closed

Applying sort to a count query fails #3914

sabrehagen opened this issue Feb 25, 2016 · 6 comments
Milestone

Comments

@sabrehagen
Copy link

To generate my queries I chain on options, for example:

const query = model.find(criteria);
if (data.select) {
    query.select(data.select);
}
if (data.sort) {
    query.sort(data.sort);
}
return query;

Now I want to add an option for count in my query builder, for example:

const query = data.count ? model.count(criteria) : model.find(criteria);
if (data.select) {
    query.select(data.select);
}
if (data.sort) {
    query.sort(data.sort);
}
return query;

However, when applying a .sort() to a .count() query, no result is returned. Given that sorting has no influence on the number of matching documents, it should be ignored at the very least, certainly it shouldn't cause the query to return undefined as it currently does.

Furthermore, as an aside to this issue, mongoose baulks if you supply a .select() to a .count() with the error Unhandled rejection Error: select cannot be used with count. Given that select doesn't influence count either, I propose it should silently fail. Thoughts?

Here's code to demonstrate the issue:

const mongoose = require('mongoose');
mongoose.set('debug', true);

mongoose.connect('mongodb://localhost:27017/sort-count');

const s = mongoose.Schema({
  v: { type: String, default: 'mongoose' }
});

const model = mongoose.model('s', s);

model.create({}, () => {
    model.create({}, () => {
        model.create({}, () => {
            model.count({}).exec((err, n) => console.log(err, n));          // works
            model.count({}).sort().exec((err, n) => console.log(err, n));   // fails
        });
    });
});
@vkarpov15
Copy link
Collaborator

Thanks for reporting. What version of mongoose are you using? Please tell me you upgraded from 3.8.x :)

@vkarpov15 vkarpov15 added this to the 4.4.6 milestone Feb 25, 2016
@sabrehagen
Copy link
Author

Yes, this is with 4.3.6.

vkarpov15 added a commit to mongoosejs/mquery that referenced this issue Mar 1, 2016
vkarpov15 added a commit to mongoosejs/mquery that referenced this issue Mar 1, 2016
vkarpov15 added a commit that referenced this issue Mar 1, 2016
@vkarpov15
Copy link
Collaborator

Fixed in cee7545

@sabrehagen
Copy link
Author

Cool, thanks. That deals with the sort issue, what's the decision on the select issue?

@vkarpov15
Copy link
Collaborator

No the above commits allow you to do both, see 139a90b

@sabrehagen
Copy link
Author

Great, thank you.

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