Find query with _start and _limit

System Information
  • Strapi Version: 3.4.1
  • Operating System: Windows
  • Database: MongoDB
  • Node Version: 14.15.1
  • NPM Version: 6.14.8
  • Yarn Version: 1.22.4

Hello everyone, could somebody help with below find query. I have a custom controller, in which i have a find query defined, now i have requirement to query to execute this based on _start and _limit parameters for pagination.

This works fine with default get API. But i need it working for this one.

entityS = await strapi.query(‘subscriptions’).model.find({_start:0,_limit:10},{
api_id:1}

Please let me know your comments.

Kind REgards,
Basu

I think this needs to be like this

entityS = await strapi.query(‘subscriptions’).find({_start:0,_limit:10, api_id:1}) 
1 Like

Thank you Dhruv for he quick response. I am getting an error as below:

My actual query goes like this:
entityS = await strapi.query(‘subscriptions’).model.find({$and:[{enterprise_id:ctx.query.enterprise_id},{$or:[{subscription_status:‘ToBeSubscribed’},{subscription_status:‘ToBeUnsubscribed’}]}]},{
_start:0,_limit:10,
api_id:1}

Any other hints?

Can you let me know in brief on actually what you are doing with subscription collection. Like a detailed explanation, then I can try to build the query

For what i’ve gathered you are trying to find

all rows of subscription that has enterpriceID === ctx.query.enterprise_id AND it’s subscription_status is either ToBeSubscribed OR ToBeUnsubscribed and then paginating it with start and limit but what about this api_id:1

If api_id needs to be 1 all the time we can add it to AND condition ??

Yes Dhruv, Your explanation is correct.

{api_id:1} definition is for retrieving the only selected fields from the collection, i have around 30 fields defined, i would only need 10 out of them. I am adding whole original query below:

entityS = await strapi.query(‘subscriptions’).model.find({$and:[{enterprise_id:ctx.query.enterprise_id},{$or:[{subscription_status:‘ToBeSubscribed’},{subscription_status:‘ToBeUnsubscribed’}]}]},{
api_id:1,
consumer_id:1,
plan_id:1,
section_id:1,
subscription_requested_date:1,
unsubscription_requested_date:1,
consumption_start_date: 1,
consumption_end_date:1,
approved_date:1,
rejected_date:1,
subscription_status:1,
subscription_processed_by:1,
unsubscription_processed_by:1,
//Below fields added for Admin history list
api_type:1,
comment:1
});

in case that anyone is looking for a full solution. This one works for Strapi v3.
//////////// put below code into services.js to extend your controller functionality:

‘use strict’;

const _ = require(‘lodash’);

module.exports = {
async find(params, populate) {
const results = await strapi.query(‘restaurant’).find({ …params, _limit: -1 }, populate);
return results;
},
};