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:
- Create a new Project and Bucket after logging in to your Cosmic dashboard.
- Go to Bucket Settings > Import / Export and install this Bucket export file.
- In the following examples, switch
YOUR_BUCKET_SLUG
andYOUR_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.
- Node.js
- cURL
- GraphQL
- CLI
Step 1. Run the following commands to add the neccessary folder structure and dependencies.
mkdir cosmic-examplecd cosmic-exampleyarn add cosmicjs
Step 2.
Create index.js
file with the following code.
const api = require('cosmicjs')() // empty initconst bucket = api.bucket({slug: 'docs-ecommerce-app-production', // YOUR_BUCKET_SLUGread_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.objectsconsole.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
.
- Node.js
- cURL
- GraphQL
- CLI
Step 1. Run the following commands to add the neccessary folder structure and dependencies.
mkdir cosmic-examplecd cosmic-exampleyarn add cosmicjs
Step 2.
Create index.js
file with the following code.
const api = require('cosmicjs')() // empty initconst bucket = api.bucket({slug: 'docs-ecommerce-app-production', // YOUR_BUCKET_SLUGread_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 propertieslimit: 3})const products = data.objectsconsole.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.
- Node.js
- cURL
- GraphQL
- CLI
Step 1. Run the following commands to add the neccessary folder structure and dependencies.
mkdir cosmic-examplecd cosmic-exampleyarn add cosmicjs
Step 2.
Create index.js
file with the following code.
const api = require('cosmicjs')() // empty initconst bucket = api.bucket({slug: 'docs-ecommerce-app-production', // YOUR_BUCKET_SLUGread_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 propertieslimit: 3})const products = data.objectsconsole.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.
- Node.js
- cURL
- GraphQL
- CLI
Step 1. Run the following commands to add the neccessary folder structure and dependencies.
mkdir cosmic-examplecd cosmic-exampleyarn add cosmicjs
Step 2.
Create index.js
file with the following code.
const api = require('cosmicjs')() // empty initconst bucket = api.bucket({slug: 'docs-ecommerce-app-production', // YOUR_BUCKET_SLUGread_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.objectsconsole.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.
- Node.js
- cURL
- GraphQL
- CLI
Step 1. Run the following commands to add the neccessary folder structure and dependencies.
mkdir cosmic-examplecd cosmic-exampleyarn add cosmicjs
Step 2.
Create index.js
file with the following code.
const api = require('cosmicjs')() // empty initconst bucket = api.bucket({slug: 'docs-ecommerce-app-production', // YOUR_BUCKET_SLUGread_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.objectsconsole.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.
- Node.js
- cURL
- GraphQL
- CLI
Step 1. Run the following commands to add the neccessary folder structure and dependencies.
mkdir cosmic-examplecd cosmic-exampleyarn add cosmicjs
Step 2.
Create index.js
file with the following code.
const api = require('cosmicjs')() // empty initconst bucket = api.bucket({slug: 'docs-ecommerce-app-production', // YOUR_BUCKET_SLUGread_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.objectsconsole.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.