Perform Simple and Compound queries
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
:
db.collection('NodeJSDb1').where("age", ">", 16).get().then((response) => {
console.log(response.data);
}).catch((error) => {
console.log(error);
});
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 |
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:
db.collection('NodeJSDb1').where("name", "!=", "Atharva").get().then((response) => {
console.log(response.data);
}).catch((error) => {
console.log(error);
});
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
:
db.collection('NodeJSDb1').where("age", ">", 16).where("age", "==", "Atharva").join("and").get().then((response) => {
console.log(response.data);
}).catch((error) => {
console.log(error);
});
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
:
db.collection('NodeJSDb1').where("age", ">", 16).where("age", "==", "Atharva").join("or").get().then((response) => {
console.log(response.data);
}).catch((error) => {
console.log(error);
});
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.
db.collection('NodeJSDb1').where("age", ">", 16).where("age", "==", "Atharva").join("or")
.orderBy("age", "asc").get().then((response) => {
console.log(response.data);
}).catch((error) => {
console.log(error);
});
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.
db.collection('NodeJSDb1').where("age", ">", 16)
.where("age", "==", "Atharva").join("or")
.orderBy("age", "asc")
.limit(10).get().then((response) => {
console.log(response.data);
}).catch((error) => {
console.log(error);
});
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.
db.collection('NodeJSDb1').where("age", ">", 16)
.where("age", "==", "Atharva").join("or")
.orderBy("age", "asc")
.limit(10)
.startAt(10)
.get().then((response) => {
console.log(response.data);
}).catch((error) => {
console.log(error);
});
Was this page helpful?