Reputation: 525
How can I tell if there are more results from a query in MongoDB?
I need to know if there are more documents after the limit query just like the question linked above, but in Ruby Mongoid.
Is there any method equivalent to hasNext for Mongoid?
UPDATE : Updating my question to give some context.
I'm trying to implement pagination but my
Here's a part of my API logic :
limit = params[:limit] + 1
query_conditions << { :_id.lt => params[:flag] }
@query = Notification.where({ '$and' => query_conditions }).limit(limit)
@last_page = @notifications_query.count < limit
I would be sending out the first (n-1) elements of the query result and last_page boolean to let the client know if this is the last page. But I would like to know if there is any built-in function or a smarter way of determining the last page boolean.
Upvotes: 1
Views: 250
Reputation: 6121
In that case, I would totally recommend kaminari-mongoid
gem. It provides various helper methods to implement pagination (docs), in your case (just a rough sketch, as I don't have access to mongodb)
@query = Notification.where({ '$and' => query_conditions }).page(params[:page] || 1).per(params[:limit].to_i + 1)
@last_page = [email protected]_page
Upvotes: 1