Skip to content

How to query

So we have two ways of quering for content using uHeadless. Both are based on the same principle of key => value matching.

The simple way to query

The simple way to query is to use a key and string value to make a query for a document in the database.

Example

ts
const { data: documents } = await useUheadless('query', {
  query: {
    contentTypeAlias: 'document',
    creatorId: '2',
  },
});

So with the simple query interface the values can only be strings (uHeadless tries to cleaverly cast them to the correct type). But it's limited to match is key equal to value. This is often the fastest though. So what i lacks in flexibility it deliveres in speed.

The advanced flexible way to query

So the last option is to use the uheadlessQuery type. With this method you have access to different matching methods, that can help you narrow down them results.

Example

Get all nodes of type document where creatorId is not 1 and cratedAt is greater than or equal to Now.

ts
const { data: documents } = await useUheadless('query', {
  query: {
    contentTypeAlias: {
      eq: 'document',
    },
    creatorId: {
      ne: 1,
    },
    createdAt: {
      gte: new Date(),
    },
  },
});

Complete list of query matching methods

MethodAccepted inputsDescription
allArray<string | number | Date | boolean>Selects the documents where the value of a field is an array that contains all the specified elements.
eqstring number Date booleanMatches documents where the value of a field equals the specified value.
existsbooleanMatches documents that contain or do not contain a specified field, including documents where the field value is null.
gtstring number Date booleanSelects those documents where the value of the specified field is greater than (i.e. >) the specified value.
gtestring number Date booleanSelects the documents where the value of the specified field is greater than or equal to (i.e. >=) a specified value (e.g. value.)
inArray<string number, date, boolean>Selects the documents where the value of a field equals any value in the specified array.
ltstring number Date booleanSelects the documents where the value of the field is less than (i.e. <) the specified value.
ltestring number Date booleanSelects the documents where the value of the field is less than or equal to (i.e. <=) the specified value.
nestring number Date booleanSelects the documents where the value of the specified field is not equal to the specified value. This includes documents that do not contain the specified field.
ninArray<string number, date, boolean>Selects the documents where the specified field value is not in the specified array or the specified field does not exist.
sizenumberMatches any array with the number of elements specified by the argument.
typestring or Array<string>selects documents where the value of the field is an instance of the specified BSON type(s)

Only include what you need

Sometimes you may want to have some related data for a node in order to create the view that you need. But often, you don't need all of the properties of the node and end up with both slow and large responses.

In this case the include and exclude options are great.

Examples

Get all nodes of type document but only include the nodeName and properties.title.

ts
const { data: documents } = await useUheadless('query', {
  include: ['nodeName', 'propertes.title'],
  query: {
    contentTypeAlias: {
      eq: 'document',
    },
  },
});

Get all nodes of type document but exclude the blocks property.

ts
const { data: documents } = await useUheadless('query', {
  exclude: ['propertes.blocks'],
  query: {
    contentTypeAlias: {
      eq: 'document',
    },
  },
});

← See more of the different options available when quering for content