Queries

Learn about queries; the way to fetch content from your Cosmic Bucket.

Prerequisite

The following references assume you have already reviewed the methods for fetching Objects and media from your Bucket.

Queries in the browser URL

Object properties

You can compose queries to search by the following Object properties.

ParameterDescription
idObject id
titleObject title
slugObject slug
typeObject type Slug
localeObject locale
created_atObject created at date
published_atObject published at date
modified_atObject modified at date
created_byObject created by user id
modified_byObject modified by user id
assigned_toObject assigned to user id
metadata.$keyMetadata value(s)

Media properties

Construct queries to search by the following media properties.

ParameterDescription
idMedia id
nameMedia name
original_nameMedia original name
sizeMedia size
typeMedia type
createdMedia created date
folderMedia folder slug. Matches that folder only, including a nested folder. Does not match by path.
metadataMedia metadata (JSON)

Selectors

Use the following selectors to build your queries.

ParameterDescription
$eqMatches values that are equal to a specified value. Default key/value query.
$gtMatches values that are greater than a specified value.
$gteMatches values that are greater than or equal to a specified value.
$ltMatches values that are less than a specified value.
$lteMatches values that are less than or equal to a specified value.
$inMatches any of the values specified in an array.
$allMatches all of the values specified in an array.
$neMatches all values that are not equal to a specified value.
$ninMatches none of the values specified in an array.

Evaluation

ParameterDescription
$regex, $optionsSearch for string, use $options: "i" for case insensitive matches.

Logic Operators

ParameterDescription
$andReturns results that match all of the query conditions.
$notReturns results that do not match the query expression.
$orReturns results that match any of the specified conditions.
$norReturns results that fail to match any of the specified conditions.

Basic examples

The following examples use the NPM module and assume you have an understaning of authentication.

A basic query may be when you want to fetch Objects in a certain type. For example, blog posts:

const query = {
  type: 'posts',
};
await cosmic.objects.find(query);

You can also use queries to fetch media.

const query = {
  type: 'video/mp4', // fetch all video files
};
await cosmic.media.find(query);

Advanced examples

Let's say you want to fetch only blog posts with a certain text metafield by a key and value:

const query = {
  type: 'posts',
  'metadata.some_unique_key': 'some-unique-value',
};
await cosmic.objects.find(query);

Let's say you want to fetch only blog posts from a certain category set by a single Object relationship metafield:

const query = {
  type: 'posts',
  'metadata.category': 'category-object-id', // The category Object id
};
await cosmic.objects.find(query);

Let's say you want to fetch products that have a price (Number Metafield) less than 100:

const query = {
  type: 'products',
  'metadata.price': {
    $lt: 100,
  },
};
await cosmic.objects.find(query);

Let's say you want to fetch products that have any of a certain set of tags (select dropdown Metafield):

const query = {
  type: 'products',
  'metadata.tags': {
    $in: ['Clothing', 'Menswear'],
  },
};
await cosmic.objects.find(query);

A repeater holds rows. Pass an object of that row's fields, and every field has to match the same row. A color on one row and a size on another does not match.

const query = {
  type: 'products',
  'metadata.variants': {
    color: 'red',
    size: '5.5',
    stock_online: {
      $gt: 0,
    },
  },
};
await cosmic.objects.find(query);

Each repeater field takes a value, or $gt, $gte, $lt, $lte, $eq, $ne, $in, and $nin. $regex is not accepted on a repeater row. A number comparison matches a number, so a numeric string does not satisfy $gt. The response still includes the full repeater: the filter decides which Objects match, and it does not remove the other rows.

Let's say you want to fetch blog posts assigned to one of a set of users (by user id) using the assigned_to property:

const query = {
  type: 'posts',
  assigned_to: {
    $in: ['user-id-1', 'user-id-2'],
  },
};
await cosmic.objects.find(query);

Let's say you want to fetch products that match a combination of values using the $and logic operator.

const query = {
  type: 'products',
  $and: [
    {
      'metadata.category': 'Tshirt',
    },
    {
      'metadata.price': {
        $lte: 29,
      },
    },
  ],
};
await cosmic.objects.find(query);

Using the $regex method we can create a search feature that searches Objects by title.

const query = {
  type: 'products',
  title: {
    $regex: 'Hoodie',
    $options: 'i', // case insensitive
  },
};
await cosmic.objects.find(query);

More query info

FAQ

How much does Cosmic cost?

Project plans are Free ($0), Builder ($99/mo), Team ($299/mo), Business ($499/mo), Enterprise (custom pricing). Yearly billing saves 10%. Workspace plans are Small Workspace ($999/mo) and Large Workspace ($2,999/mo), and they start with a 30-day free trial. Allowances, add-on rates, and the rest of the pricing FAQ are plain text at https://www.cosmicjs.com/pricing.md.

Are API keys required, and is content private?

Each Bucket has a read key and a write key in Bucket Settings > API Access. GET requests use the read key. POST, PATCH, and DELETE use the write key as Authorization: Bearer. Do not put the write key in a browser bundle, a mobile app, or a NEXT_PUBLIC_ variable. SAML single sign-on is on Large Workspace and Enterprise. The audit log is append-only. Text versions: https://www.cosmicjs.com/security.md and https://www.cosmicjs.com/docs/dashboard/audit-log.md.

Can an agent create a Cosmic project without an existing account?

Yes. POST https://dapi.cosmicjs.com/v3/agents/sign-up provisions a free-tier project and bucket tied to a human email, with no existing Cosmic account. The human claims it with the emailed one-time code. The playbook is https://www.cosmicjs.com/docs/ai-onboarding.md.