Query Data
Perform Simple and Compound queries'
Powerful query functionality for specifying which documents you want to retrieve from a Collection
WorqDB provides powerful query functionality for specifying which documents you want to retrieve from a collection or collection group.
Simple Queries
Simple queries are useful when you know the exact value you want to match for a specific field or fields.
For example, the following query returns all documents where the name
field exactly matches John Doe
:
curl --location 'https://api.worqhat.com/api/collections/data/fetch/query' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <token>' \
--data '{
"collection": "Sample collection",
"queries": [
{
"field": "name",
"operator": "==",
"value": "John Doe"
}
]
}'
Query Operators
WorqDB supports a variety of query operators:
Operator | Description |
---|---|
== |
Equal to |
!= |
Not equal to |
< |
Less than |
<= |
Less than or equal to |
> |
Greater than |
>= |
Greater than or equal to |
less than |
Less than |
less than or equal to |
Less than or equal to |
equal to |
Equal to |
greater than |
Greater than |
greater than or equal to |
Greater than or equal to |
not equal to |
Not equal to |
array-contains |
Checks an array for particular content |
Not Equal To Operator (!=
)
Use the not equal (!=) operator to return documents where the given field exists and does not match the comparison value. For example:
curl --location 'https://api.worqhat.com/api/collections/data/fetch/query' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <token>' \
--data '{
"collection": "Sample collection",
"queries": [
{
"field": "capital",
"operator": "!=",
"value": "Mumbai"
}
]
}'
This query returns every city document where the capital
field exists with a value other than Mumbai
.
Compound Queries (AND)
You can also chain multiple simple queries together using logical AND operators. For example, the following query returns all documents where the name
field exactly matches John Doe
and the capital
field exactly matches Mumbai
:
curl --location 'https://api.worqhat.com/api/collections/data/fetch/query' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <token>' \
--data '{
"collection": "Sample collection",
"queries": [
{
"field": "capital",
"operator": "==",
"value": "Mumbai"
},
{
"field": "name",
"operator": "==",
"value": "John Doe"
}
],
"compounding": "AND",
}'
Compound Queries (OR)
You can also chain multiple simple queries together using logical OR operators. For example, the following query returns all documents where the name
field exactly matches John Doe
or the capital
field exactly matches Mumbai
:
```shell
curl --location 'https://api.worqhat.com/api/collections/data/fetch/query' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <token>' \
--data '{
"collection": "Sample collection",
"queries": [
{
"field": "capital",
"operator": "==",
"value": "Mumbai"
},
{
"field": "name",
"operator": "==",
"value": "John Doe"
}
],
"compounding": "AND",
}'
Order data
You can also order the data by a field. For example, the following query returns all documents where the name
field exactly matches John Doe
and the capital
field exactly matches Mumbai
and the data is ordered by the timestamp
field. By default, a query retrieves all documents that satisfy the query in ascending order by document ID.
curl --location 'https://api.worqhat.com/api/collections/data/fetch/query' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <token>' \
--data '{
"collection": "Sample collection",
"queries": [
{
"field": "capital",
"operator": "==",
"value": "Mumbai"
},
{
"field": "name",
"operator": "==",
"value": "John Doe"
}
],
"compounding": "AND",
"orderBy": "docCreatedAt",
"orderType": "desc",
}'
orderBy
- The field to order the data by. Defaults todocCreatedAt
.orderType
- The order type. Defaults toasc
. You can selectasc
ordesc
.
orderBy()
clause also filters for existence of the given field. The result set will not include documents that do not contain the given field.
Limit data
You can also limit the data returned by a query. For example, the following query returns all documents where the name
field exactly matches John Doe
and the capital
field exactly matches Mumbai
and the data is ordered by the timestamp
field and the data is limited to 10 documents.
curl --location 'https://api.worqhat.com/api/collections/data/fetch/query' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <token>' \
--data '{
"collection": "Sample collection",
"queries": [
{
"field": "capital",
"operator": "==",
"value": "Mumbai"
},
{
"field": "name",
"operator": "==",
"value": "John Doe"
}
],
"compounding": "AND",
"orderBy": "docCreatedAt",
"orderType": "desc",
"limit": 10,
}'
Pagination (startAfter)
You can also paginate the data returned by a query. For example, the following query returns all documents where the name
field exactly matches John Doe
and the capital
field exactly matches Mumbai
and the data is ordered by the timestamp
field and the data is limited to 10 documents and the data is paginated by the startAfter
field. The startAfter
field is an integer
value that you can pass in to get the next set of data.
curl --location 'https://api.worqhat.com/api/collections/data/fetch/query' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <token>' \
--data '{
"collection": "Sample collection",
"queries": [
{
"field": "capital",
"operator": "==",
"value": "Mumbai"
},
{
"field": "name",
"operator": "==",
"value": "John Doe"
}
],
"compounding": "AND",
"orderBy": "docCreatedAt",
"orderType": "desc",
"limit": 10,
"startAfter": 10,
}'