WorqDB provides powerful query functionality for specifying which documents you want to retrieve from a collection or collection group.

You can combine where() filters with orderBy(), limit() and startAfter(). Any combination of queries can work for all cases. In case you want to make it simpler, you can go ahead and try the Natural Language Queries to run complex queries using Natural Language.

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:

OperatorDescription
==Equal to
!=Not equal to
<Less than
<=Less than or equal to
>Greater than
>=Greater than or equal to
less thanLess than
less than or equal toLess than or equal to
equal toEqual to
greater thanGreater than
greater than or equal toGreater than or equal to
not equal toNot 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.

A != query clause might match many documents in a collection. To control the number of results returned, use a limit clause or paginate your query.

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 to docCreatedAt.
  • orderType - The order type. Defaults to asc. You can select asc or desc.
An 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);
});
You can combine where() filters with orderBy() and limit().

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);
});