No history yet

Advanced Query Syntax

Crafting Complex Queries

Simple key-value searches are a great start, but real-world data often requires more nuance. To ask more specific questions, you need to combine multiple conditions. This is where logical operators come in, allowing you to build precise, powerful queries that filter your data exactly how you need it.

Logical Operators

At the core of complex queries are three fundamental logical operators: $and, $or, and $not. These allow you to chain together different conditions to broaden or narrow your search results.

The $and operator is the default for combining selectors, but explicitly stating it can make complex queries clearer. It ensures that all specified conditions must be true for a document to be returned. Think of it as a strict checklist; every box must be ticked.

{
  "selector": {
    "$and": [
      { "year": { "$gt": 2010 } },
      { "genre": { "$eq": "sci-fi" } }
    ]
  }
}

This query finds all documents where the year is after 2010 and the genre is exactly "sci-fi".

Conversely, the $or operator is more flexible. It returns documents that match at least one of the conditions in the list. It's perfect for when you're looking for items that could fall into one of several categories.

{
  "selector": {
    "$or": [
      { "status": "published" },
      { "status": "archived" }
    ]
  }
}

This will return documents where the status is either "published" or "archived".

Finally, the $not operator inverts the logic of a condition, excluding any documents that match it. It’s useful for filtering out specific items from a broader result set.

{
  "selector": {
    "author": { "$not": { "$eq": "Jane Doe" } }
  }
}

This query finds all documents where the author is not Jane Doe.

Querying Within a Range

Sometimes you don't need an exact match but rather data that falls within a certain range. This is common for numbers, dates, and even alphabetically sorted strings. Cloudant provides several operators for this:

  • $lt: Less than
  • $lte: Less than or equal to
  • $gt: Greater than
  • $gte: Greater than or equal to

You can combine these to define a specific window for your search.

{
  "selector": {
    "price": {
      "$gte": 19.99,
      "$lte": 99.99
    }
  }
}

This query finds all documents where the price field is between $19.99 and $99.99, inclusive. This single selector implicitly uses an $and condition; both the $gte and $lte checks must pass.

Range operators let you search for values 'less than', 'greater than', or 'between' specific points.

Sorting Your Results

Getting the right data is only half the battle. Presenting it in a useful order is just as important. The sort syntax allows you to define how your results should be ordered.

You can sort by a single field or create a more complex sort by providing an array of fields. The database will sort by the first field, then use the second to break any ties, and so on.

{
  "selector": {
    "type": "article"
  },
  "sort": [
    { "publication_date": "desc" },
    { "author_lastname": "asc" }
  ]
}

In this example, the results are first sorted by publication_date in descending order (desc), showing the newest articles first. If multiple articles share the same date, they are then sorted by author_lastname in ascending alphabetical order (asc).

To ensure good performance, any fields you use for sorting must be included in the corresponding index.

Now let's see how you can apply these advanced techniques.

Quiz Questions 1/6

Which logical operator returns documents that match at least one of the specified conditions?

Quiz Questions 2/6

How do you construct a query to find all products with a price greater than 50 but less than or equal to 100?