Back to changelog
Changelog

Introducing Advanced Queries

Tony Spiro's avatar

Tony Spiro

June 10, 2020

cover image

We are excited to announce the release of Advanced Queries (Beta) now available for all accounts. This is huge for teams that would like to use Cosmic beyond basic content management, enabling laser-focused logic to deliver content.

How it works
Advanced queries give you powerful NoSQL database-like functionality for data fetching. Using the Cosmic REST API endpoint and available clients (including the Cosmic GraphQL API) you can customize your NoSQL-style query (in JSON format) to customize selectors and query logic. See below examples and the docs for more info.





Examples

To keep things concise, use the following bucket variable for the following examples.

const bucket = Cosmic.bucket({
  slug: 'bucket-slug',
  read_key: "your-read-key-found-in-bucket-settings"
});


Match Objects with exact title

bucket.getObjects({
  type: 'posts',
  props: 'slug,title,content',
  query: {
    "title": "Post 1"
  }
});


Match Objects greater than or equal to metadata value

bucket.getObjects({
  type: 'posts',
  props: 'slug,title,content',
  query: {
    "metadata.price": {
      "$gte": 9.99
    }
  }
});


Match Objects with nested JSON metadata value (JSON Metafield)

bucket.getObjects({
  type: 'posts',
  props: 'slug,title,content',
  query: {
    "metadata.json_data": {
      "is_awesome": true,
      "other_data": {
        "nested": "yep"
      }
    }
  }
});


Match Objects with any metadata values

bucket.getObjects({
  type: 'posts',
  props: 'slug,title,content',
  query: {
    "$or": [
      {
         "metadata.grade": "A"
      },
      {
        "metadata.grade": "B"
      }
    ]
  }
});


Match Objects with string in content using a regular expression. Case insensitive with $options.

bucket.getObjects({
  type: 'posts',
  props: 'slug,title,content',
  query: {
    "content": {
      "$regex": "jamstack",
      "$options": "i"
    }
  }
});


Match Objects with any Multiple Object Metafield values

bucket.getObjects({
  type: 'posts',
  props: 'slug,title,content',
  query: {
    "metadata.categories": {
      "$in": ["category_id-1","category_id-2"]
    }
  }
});


Match Objects that don't have any Multiple Object Metafield values

bucket.getObjects({
  type: 'posts',
  props: 'slug,title,content',
  query: {
    "metadata.categories": {
      "$nin": ["category_id-1","category_id-2"]
    }
  }
});

See the docs for the full list of query selectors and logic operators.


Advanced Queries in the GraphQL API
You may need to set the variable outside of the main query area. Add something like this to the Query Variables area:

{
  "query": {
    "title": {
      "$regex": "another",
      "$options": "i"
    }
  }
}


View full screen  


We hope you enjoy using Advanced Queries to give you more power and flexibility to get your Cosmic content. This is a Beta release and we would like your feedback as we improve this feature, so please let us know what you think! Reach out to us on Twitter and join the conversation in Slack.