Mongoose: limit and skip doesn't work on mongodb 3.2

Created on 15 Jan 2016  ·  12Comments  ·  Source: Automattic/mongoose

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

won't fix

Most helpful comment

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

All 12 comments

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) ?

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.

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.

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

It's a won't fix @rgalindo33

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

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)
      });

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.

@markstos please do, I appreciate your help :+1:

Ah, sorry, looks like I don't have edit rights on the wiki:
https://github.com/Automattic/mongoose/wiki/4.0-Release-Notes Perhaps it's
restricted to collaborators only.

At least it should be a quick copy/paste to add the text I wrote.

On Fri, Jan 5, 2018 at 8:10 PM Valeri Karpov notifications@github.com
wrote:

@markstos https://github.com/markstos please do, I appreciate your help
👍


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/Automattic/mongoose/issues/3763#issuecomment-355710418,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AABk5ZivjBHjh2puqlBeMjSLUIl5qUs4ks5tHsfugaJpZM4HFibq
.

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

Was this page helpful?
0 / 5 - 0 ratings