Skip to content

limit and skip doesn't work on mongodb 3.2 #3763

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
pongib opened this issue Jan 15, 2016 · 12 comments
Closed

limit and skip doesn't work on mongodb 3.2 #3763

pongib opened this issue Jan 15, 2016 · 12 comments

Comments

@pongib
Copy link

pongib commented Jan 15, 2016

It seem like mongodb 3.2, limit and skip field must be numeric number. Can you embed cast string to int into your mongoose.

@vkarpov15
Copy link
Collaborator

limit() and skip() explicitly expect a number in the mongoose docs and we don't currently support any casting. Do you want .limit('10'); to work like .limit(10) ?

@pongib
Copy link
Author

pongib commented Jan 17, 2016

Thank you for your reply. I use limit and skip as options parameter like this
Product.find({}, '-__v -updatedAt', { lean: true, skip: skip, limit: limit, sort: sort }, function (err, results) { do stuff }), everything work fine in mongo 3.0 but when I change to 3.2, error occur and display something like skip and limit must be numeric. But when I cast to numeric everything work well. It ok if I just cast to number, But I curious why it work on mongo 3.0 but doesn't work on mongo 3.2. I use mongoose 4.3.5. Thank you.

@vkarpov15
Copy link
Collaborator

Same thing happens in the shell:

$ ~/Workspace/10gen/mongodb-test-makefile/3.0.5/bin/mongo
MongoDB shell version: 3.0.5
connecting to: test
Server has startup warnings: 
2016-01-18T09:22:16.328-0500 I CONTROL  [initandlisten] 
2016-01-18T09:22:16.328-0500 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-01-18T09:22:16.328-0500 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-01-18T09:22:16.328-0500 I CONTROL  [initandlisten] 
2016-01-18T09:22:16.328-0500 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-01-18T09:22:16.328-0500 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-01-18T09:22:16.328-0500 I CONTROL  [initandlisten] 
> db.test.find().limit('10');
> ^C
bye
$ ~/Workspace/10gen/mongodb-test-makefile/3.2.0/bin/mongo
MongoDB shell version: 3.2.0
connecting to: test
Server has startup warnings: 
2016-01-18T09:22:54.881-0500 I CONTROL  [initandlisten] 
2016-01-18T09:22:54.881-0500 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-01-18T09:22:54.881-0500 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-01-18T09:22:54.881-0500 I CONTROL  [initandlisten] 
2016-01-18T09:22:54.881-0500 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-01-18T09:22:54.881-0500 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-01-18T09:22:54.881-0500 I CONTROL  [initandlisten] 
> db.test.find().limit('10');
Error: error: {
    "waitedMS" : NumberLong(0),
    "ok" : 0,
    "errmsg" : "Failed to parse: { find: \"test\", filter: {}, limit: \"10\", singleBatch: false }. 'limit' field must be numeric.",
    "code" : 9
}

I opened up https://jira.mongodb.org/browse/DOCS-6995 because this should be documented on the mongodb server.

@rgalindo33
Copy link

was this issue solved? im having the same problems and would like to know the status of it

@vkarpov15
Copy link
Collaborator

It's a won't fix @rgalindo33

@StefanBalea
Copy link

/create user query/
let userQuery = UserModel
.find({})
.limit(Number(limit));
Casting to Number fixed the problem.

@zahid492
Copy link

zahid492 commented Feb 26, 2017

This code work for me mongo 3.28


Delivery.find({}, '-__v').limit(parseInt(req.params.limit)).skip(parseInt(req.params.skip)* parseInt(req.params.limit)).sort({
        _id: -1
      }).exec(function(err, data) {
        if (err) return res.json({
          'error': err
        })
        return res.json(data)
      });

@markstos
Copy link
Contributor

markstos commented Jan 5, 2018

I also ran into this change when upgrading from Mongoose 3.8 to Mongoose 4.13 when running MongoDB 3.2. Mongoose 3.8 would accept strings for limit() and skip(), while the same code breaks with Mongoose 4.13 with a parse error. Both Mongoose 3.8 and Mongoose 4.13 document that skip() and limit()` accept a number as input, so it's not a backwards-incompatible change according to the docs, but the difference could still break some apps that depended on the looser behavior of Mongoose 3.8. For this reason, I think it's worth a mention in the 4.0 release notes under Backwards-Breaking Changes (which is my guess at when the behavior change was introduced):

#3763: limit() and skip() now require their input to be numbers. Mongoose 3.8 was already documented to require numbers here, but previously strings were accepted and cast to numbers. Passing numbers as strings will now throw a parse error.

If that's acceptable I can make the addition to the wiki page.

@vkarpov15
Copy link
Collaborator

@markstos please do, I appreciate your help 👍

@markstos
Copy link
Contributor

markstos commented Jan 6, 2018 via email

@vkarpov15
Copy link
Collaborator

@lukegorman
Copy link

parseInt works...
.
Publication.find({}).skip(parseInt(query.skip))
.toArray(function(err, result){
.
.

@Automattic Automattic locked as resolved and limited conversation to collaborators Sep 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants