Cloudant fundamentals: Querying in Node.js

Simple queries via npm package designed for new Cloudant users (part 8 of 10)

In the previous part of this series we discovered the _find endpoint which allows us to formulate queries in JSON and ask Cloudant to answer them.

In this post, we’ll look to doing the same thing but using Node.js code. Again we’ll lean on the cloudant-quickstart library. The examples here assume you have followed the basic npm install and configuration functions found on the package's page.

A convenient npm package (cloudant-quickstart), and a convenience store. Photo by Fancycrave on Unsplash.

Making queries

Using the query we made last time, we can pass the selector directly to the query function of the cloudant-quickstart object to get an array of matching documents back.

We can do the same queries with additional clauses:


If we need to sort, we can pass in the sort options as a second parameter:

SQL too

If you prefer to express your queries in Structured Query Language (SQL), then the cloudant-quickstart library can understand that too:

The cloudant-quickstart library converts your SQL into the equivalent Cloudant Query object for you. You can see the object by calling the explain function:

To be clear, this SQL-to-query conversion only works for non-aggregating SELECT queries, but it is a useful way to express your query and to understand what the equivalent Cloudant Query looks like.

Next time

Querying is only one half of the story. In order to have your queries run efficiently, your database needs its data indexing too. We’ll see what that means in the next installment.