Skip to content

Advanced Queries

Setup

The following code examples uses a demo Bucket which includes product data for an ecommerce store. You can run the code examples as is (copy + paste!) or install the demo content to your account with the following steps:

  1. Create a new Project and Bucket after logging in to your Cosmic dashboard.
  2. Go to Bucket Settings > Import / Export and install this Bucket export file.
  3. In the following examples, switch YOUR_BUCKET_SLUG and YOUR_BUCKET_READ_KEY to your Bucket access keys. You can find these values located in Bucket Settings > API Access after logging in to your Cosmic dashboard.

Get products by metadata value

Get Objects from your Cosmic Bucket using the metadata query parameter.

Step 1. Run the following commands to add the neccessary folder structure and dependencies.

mkdir cosmic-example
cd cosmic-example
yarn add cosmicjs

Step 2. Create index.js file with the following code.

const api = require('cosmicjs')() // empty init
const bucket = api.bucket({
slug: 'docs-ecommerce-app-production', // YOUR_BUCKET_SLUG
read_key: '49TG2OEZMVML9fNvVObbFOEsjkFaCYpbVIP1kb8kYCY6RMWqBf' // YOUR_BUCKET_READ_KEY
})
const app = async () => {
const data = await bucket.getObjects({
query: {
type: 'products',
'metadata.category':'Electronics'
},
props: 'slug,title,metadata.category' // declare your response properties
})
const products = data.objects
console.log(JSON.stringify(products, null, 2))
}
app()

Step 3. Run the app.

node index.js

Expected Result

[
{
"slug": "watch",
"title": "Watch",
"metadata": {
"category": {
"key": "electronics",
"value": "Electronics"
}
}
},
{
"slug": "battery-charger",
"title": "Battery Charger",
"metadata": {
"category": {
"key": "electronics",
"value": "Electronics"
}
}
},
{
"slug": "phone-case",
"title": "Phone Case",
"metadata": {
"category": {
"key": "electronics",
"value": "Electronics"
}
}
}
]

Get products by Object Metafield

Get Objects from your Bucket that match Multiple Object Metafield values.

💡

Note
This method queries the Multiple Object Metafield value by the Object id.

Step 1. Run the following commands to add the neccessary folder structure and dependencies.

mkdir cosmic-example
cd cosmic-example
yarn add cosmicjs

Step 2. Create index.js file with the following code.

const api = require('cosmicjs')() // empty init
const bucket = api.bucket({
slug: 'docs-ecommerce-app-production', // YOUR_BUCKET_SLUG
read_key: '49TG2OEZMVML9fNvVObbFOEsjkFaCYpbVIP1kb8kYCY6RMWqBf' // YOUR_BUCKET_READ_KEY
})
const app = async () => {
const data = await bucket.getObjects({
query: {
type: 'products',
'metadata.stores':'6296803e6c21c20e4add83ae' // Store Object id
},
props: 'slug,title,metadata.stores.title', // declare your response properties
limit: 3
})
const products = data.objects
console.log(JSON.stringify(products, null, 2))
}
app()

Step 3. Run the app.

node index.js

Expected Result

[
{
"slug": "watch",
"title": "Watch",
"metadata": {
"stores": [
{
"title": "Walmart"
},
{
"title": "Target"
}
]
}
},
{
"slug": "battery-charger",
"title": "Battery Charger",
"metadata": {
"stores": [
{
"title": "Walmart"
},
{
"title": "Target"
}
]
}
},
{
"slug": "pants",
"title": "Pants",
"metadata": {
"stores": [
{
"title": "Walmart"
},
{
"title": "Target"
}
]
}
}
]

Get products by multiple metadata values using $in

The following example uses the $in logic operator to search products by multiple metafield values.

Step 1. Run the following commands to add the neccessary folder structure and dependencies.

mkdir cosmic-example
cd cosmic-example
yarn add cosmicjs

Step 2. Create index.js file with the following code.

const api = require('cosmicjs')() // empty init
const bucket = api.bucket({
slug: 'docs-ecommerce-app-production', // YOUR_BUCKET_SLUG
read_key: '49TG2OEZMVML9fNvVObbFOEsjkFaCYpbVIP1kb8kYCY6RMWqBf' // YOUR_BUCKET_READ_KEY
})
const app = async () => {
const data = await bucket.getObjects({
query: {
type: 'products',
'metadata.category': {
$in: ['Clothing','Food']
}
},
props: 'slug,title,metadata.category', // declare your response properties
limit: 3
})
const products = data.objects
console.log(JSON.stringify(products, null, 2))
}
app()

Step 3. Run the app.

node index.js

Expected Result

[
{
"slug": "pants",
"title": "Pants",
"metadata": {
"category": {
"key": "clothing",
"value": "Clothing"
}
}
},
{
"slug": "blouse",
"title": "Blouse",
"metadata": {
"category": {
"key": "clothing",
"value": "Clothing"
}
}
},
{
"slug": "pineapple",
"title": "Pineapple",
"metadata": {
"category": {
"key": "food",
"value": "Food"
}
}
}
]

Get products by price using $lte

The following example uses the $lte (less than or equal to) logic operator to search products by price.

Step 1. Run the following commands to add the neccessary folder structure and dependencies.

mkdir cosmic-example
cd cosmic-example
yarn add cosmicjs

Step 2. Create index.js file with the following code.

const api = require('cosmicjs')() // empty init
const bucket = api.bucket({
slug: 'docs-ecommerce-app-production', // YOUR_BUCKET_SLUG
read_key: '49TG2OEZMVML9fNvVObbFOEsjkFaCYpbVIP1kb8kYCY6RMWqBf' // YOUR_BUCKET_READ_KEY
})
const app = async () => {
const data = await bucket.getObjects({
query: {
type: 'products',
'metadata.price': {
$lte: 10.00
}
},
props: 'slug,title,metadata.price' // declare your response properties
})
const products = data.objects
console.log(JSON.stringify(products, null, 2))
}
app()

Step 3. Run the app.

node index.js

Expected Result

[
{
"slug": "pineapple",
"title": "Pineapple",
"metadata": {
"price": 1.99
}
},
{
"slug": "sandwich",
"title": "Sandwich",
"metadata": {
"price": 8.99
}
}
]

Get products that match a combination of metadata values using $and

The following example uses the $and logic operator to search products by a combination of values.

Step 1. Run the following commands to add the neccessary folder structure and dependencies.

mkdir cosmic-example
cd cosmic-example
yarn add cosmicjs

Step 2. Create index.js file with the following code.

const api = require('cosmicjs')() // empty init
const bucket = api.bucket({
slug: 'docs-ecommerce-app-production', // YOUR_BUCKET_SLUG
read_key: '49TG2OEZMVML9fNvVObbFOEsjkFaCYpbVIP1kb8kYCY6RMWqBf' // YOUR_BUCKET_READ_KEY
})
const app = async () => {
const data = await bucket.getObjects({
query: {
type: 'products',
"$and": [
{
"metadata.category": "Food"
},
{
"metadata.price": {
"$lte": 49.99
}
}
]
},
props: 'slug,title,metadata.category,metadata.price' // declare your response properties
})
const products = data.objects
console.log(JSON.stringify(products, null, 2))
}
app()

Step 3. Run the app.

node index.js

Expected Result

[
{
"slug": "pineapple",
"title": "Pineapple",
"metadata": {
"category": {
"key": "food",
"value": "Food"
},
"price": 1.99
}
},
{
"slug": "sandwich",
"title": "Sandwich",
"metadata": {
"category": {
"key": "food",
"value": "Food"
},
"price": 8.99
}
},
{
"slug": "pasta",
"title": "Pasta",
"metadata": {
"category": {
"key": "food",
"value": "Food"
},
"price": 12.99
}
}
]

Get products not equal to a metadata value using $ne

The following example uses the $ne logic operator to search products not equal to a value.

Step 1. Run the following commands to add the neccessary folder structure and dependencies.

mkdir cosmic-example
cd cosmic-example
yarn add cosmicjs

Step 2. Create index.js file with the following code.

const api = require('cosmicjs')() // empty init
const bucket = api.bucket({
slug: 'docs-ecommerce-app-production', // YOUR_BUCKET_SLUG
read_key: '49TG2OEZMVML9fNvVObbFOEsjkFaCYpbVIP1kb8kYCY6RMWqBf' // YOUR_BUCKET_READ_KEY
})
const app = async () => {
const data = await bucket.getObjects({
query: {
type: 'products',
"metadata.category": {
$ne: "Food"
}
},
limit: 3,
props: 'slug,title,metadata.category,metadata.price' // declare your response properties
})
const products = data.objects
console.log(JSON.stringify(products, null, 2))
}
app()

Step 3. Run the app.

node index.js

Expected Result

[
{
"slug": "watch",
"title": "Watch",
"metadata": {
"category": {
"key": "electronics",
"value": "Electronics"
},
"price": 259.99
}
},
{
"slug": "battery-charger",
"title": "Battery Charger",
"metadata": {
"category": {
"key": "electronics",
"value": "Electronics"
},
"price": 52.99
}
},
{
"slug": "pants",
"title": "Pants",
"metadata": {
"category": {
"key": "clothing",
"value": "Clothing"
},
"price": 37.99
}
}
]

Next steps

💡

Learn More
Go to the queries section in the docs to learn more about Cosmic queries and available methods.