How it works...

With our URL parameters passed through to Mongoose, we can make a variety of queries, including pagination pages and sizes, and finding posts by title:

http://localhost:3000/api/posts?page=3

http://localhost:3000/api/posts?size=10&page=1

http://localhost:3000/api/posts?title=foobar

Even more impressive, for published dates, we can actually send an entire date range query in the JSON format as a URLEncoded query parameter, and it will return to us only the posts with publish dates between these two ISO dates. For this date range filter to work, the dates must already be formatted in ISO 8601 standard to be successfully parsed by MongoDB:

http://localhost:3000/api/posts?published={"$gte": "2017-05-11T00:08:38.317Z", "$lt": "2017-06-08T11:57:14.963Z"}

http://localhost/api/posts?published=%7B%22$gte%22:%20%222017-05-11T00:08:38.317Z%22,%20%22$lt%22:%20%222017-06-08T11:57:14.963Z%22%7D

These parameters are fed to Mongoose's query API, which provides a way of composing requests for documents data together into a single request to MongoDB. This query API is perhaps Mongoose's most powerful toolkit. It provides a robust way to associate different queries together and then execute them as a single request. It contains dozens of methods, covering everything from filtering, sorting, and manipulating document results, to geospatial, optimization, and functional utilities for querying MongoDB. Its breadth encompasses many of the features you would expect in a fully featured database without the need for an underlying structured query language.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.146.61