Mahahari
Mahahari

Reputation: 1011

To find records between a range - SAILS.JS

My requirement is to get 11th to 20th record from the database model.

For example in SQL,

  SELECT * FROM tbl LIMIT 5,10;  # Retrieve rows 6-15

How can i achieve the same in sails.js?

I used .limit(20) to limit 20records ..but if i want to retrieve 11th to 20th records only , then i tried to use .skip(10) but it didnt work.

For example,

      Table.find(query).sort(qSort).skip(10).limit(20).exec(cb);//limit 20records and skip 10 records

Please correct me if iam wrong and help me to solve the problem.

Thanks!

Upvotes: 2

Views: 736

Answers (1)

myusuf
myusuf

Reputation: 12240

To get the 11th to 20th record:

Table.find(query).sort(qSort).skip(10).limit(10).exec(cb);

How this works:

First, the result of the query is forwarded by skip (so we are at 11th record)

Then, a limit of 10 is applied (so we are left with 11th to 20th record)

So, regardless of sequence, skip is applied first to cursor, and then limit is applied on the resultant set.

Hence,

Table.find(query).sort(qSort).limit(10).skip(10).exec(cb);

would yield the same result as the previous query.

Also, you could try paginate:

Table.find().sort(qSort).paginate({page: 2, limit: 10});

Hope this helps.

Upvotes: 4

Related Questions