Turistforeningen / node-mongo-querystring

Query builder for accepting URL query parameters into your MongoDB queries. Safe and feature rich. Supports most of MongoDB's query operators such as $eq, $gt, $lt, $ne, $in, $nin, $exists, $regex, geospatial queries such as bbox and near, as well as your own custom query business logic!
MIT License
100 stars 31 forks source link

Pagination #46

Open alexborisov opened 8 years ago

alexborisov commented 8 years ago

Hey,

Any thoughts on how to implement a custom page/limit filter? Near as I can tell this itsn't handled in a normal query which gets built, but are additional operators find().limit().skip()

Starefossen commented 8 years ago

Yes, this is something I have thought of but I have not concluded on what the best API interface should look like. Any suggestions?

alexborisov commented 8 years ago

Commonly this is done via a ?limit&page. On a side note it's actually not super efficient to use skip because this is done on a cursor so the data is first fetched in it's entirety and then paged. Mongo suggests using ranged queries using either a date field or _id (which is sorted). So you would then ask for the next 10 records starting form date/id X.

Personally I think this can all get a little messy. Perhaps a better approach would be to consider doing all of this via the aggregation pipeline. The benefit of this is that your query object is an array of aggregation queries so you can build this up with the middleware and pass a single query object to mongo (for instance how would you easily handle count, skip, limit or any other cursor operation?).

You could even make this an option (so the output of this middleware will either be a simple query or a pipeline)

mhirsch commented 7 years ago

I'd love to see something like this as well. Thanks for a great project!