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
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
.
const { data: documents } = await useUheadless('query', {
query: {
contentTypeAlias: {
eq: 'document',
},
creatorId: {
ne: 1,
},
createdAt: {
gte: new Date(),
},
},
});
Complete list of query matching methods
Method | Accepted inputs | Description |
---|---|---|
all | Array<string | number | Date | boolean> | Selects the documents where the value of a field is an array that contains all the specified elements. |
eq | string number Date boolean | Matches documents where the value of a field equals the specified value. |
exists | boolean | Matches documents that contain or do not contain a specified field, including documents where the field value is null. |
gt | string number Date boolean | Selects those documents where the value of the specified field is greater than (i.e. >) the specified value. |
gte | string number Date boolean | Selects the documents where the value of the specified field is greater than or equal to (i.e. >=) a specified value (e.g. value.) |
in | Array<string number, date, boolean> | Selects the documents where the value of a field equals any value in the specified array. |
lt | string number Date boolean | Selects the documents where the value of the field is less than (i.e. <) the specified value. |
lte | string number Date boolean | Selects the documents where the value of the field is less than or equal to (i.e. <=) the specified value. |
ne | string number Date boolean | Selects 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. |
nin | Array<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. |
size | number | Matches any array with the number of elements specified by the argument. |
type | string 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.
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.
const { data: documents } = await useUheadless('query', {
exclude: ['propertes.blocks'],
query: {
contentTypeAlias: {
eq: 'document',
},
},
});
← See more of the different options available when quering for content