Skip to main content

Using Queries

With Firebase Express SDK, you can use queries to perform more customized data retrievals. Query parameters are used to filter, sort, and limit your data. Here are some query parameters you can use along with their explanations:

Without Query

Retrieve all users:

{
collection: "users",
endpoint: "/api/getusers",
request: {
type: "GET",
},
}

Filter, Order and Limit

Retrieve users with age greater than 10 and limit the result to 2:

{
collection: "users",
endpoint: "/api/getuserswithagequery",
request: {
type: "GET",
query: {
attribute: "age",
value: 10,
operator: ">",
limit: 2,
orderBy: "age",
order: "asc",
},
},
}

Filter and Limit

Retrieve users with age greater than 10 and limit the result to 2 (without ordering):

{
collection: "users",
endpoint: "/api/getuserswithagequery",
request: {
type: "GET",
query: {
attribute: "age",
value: 10,
operator: ">",
limit: 2,
},
},
}

Order and limit

Retrieve the first 2 users, ordered by age in ascending order:

{
collection: "users",
endpoint: "/api/getuserswithagequery",
request: {
type: "GET",
query: {
limit: 2,
orderBy: "age",
order: "asc",
},
},
}

As seen in the examples above, the limit parameter should be used in combination with other query parameters. This ensures effective data retrieval with specific limitations.

Note

The limit parameter cannot be used on its own without other query parameters. Be cautious when defining query parameters and create combinations that best filter, sort, and limit your data.

For detailed information on how to filter data using queries, please check the Filtering data with queries section in the official Firebase Firestore documentation.

To learn more about limiting and ordering data using queries, you can refer to the Order and limit data with queries section in the official Firebase Firestore documentation.

Working with Relationships: Using paramKey and query

In Firebase Express SDK, you can seamlessly work with related collections by utilizing the paramKey and query features. This section will guide you through the process of making GET requests with both paramKey and query for collections with relationships. We'll take an example scenario involving the "authors" and "blogs" collections to illustrate how to effectively use these features.

Utilizing paramKey for Dynamic Queries

The paramKey allows you to specify variable parameters in your request URL. For instance, let's consider a scenario where you want to retrieve all the blogs associated with a specific author using the endpoint /api/getBlogPostsByAuthorId/:authorId. In this case, authorId is the parameter supplied by the requester to identify the author.

Here's how you can use paramKey in the SDK:

firebaseStarter.addActions([
{
collection: "blogs",
endpoint: "/api/getBlogPostsByAuthorId/:authorId",
request: {
type: "GET",
paramKey: "authorId",
},
},
]);

Enhancing Queries with query

The query feature allows you to filter and sort data based on specific criteria. For example, you might want to order blogs by date or filter blogs by a particular date range. This is especially useful for retrieving related data.

Here's how you can incorporate query into the SDK:

firebaseStarter.addActions([
{
collection: "blogs",
endpoint: "/api/blogs",
request: {
type: "GET",
query: {
order: "desc",
orderBy: "date",
limit: 2,
},
},
},
]);

Combining paramKey and query for Advanced Queries

Now, let's combine both paramKey and query to fetch blogs by a specific author and order them by date:

firebaseStarter.addActions([
{
collection: "blogs",
endpoint: "/api/getBlogPostsByAuthorId/:authorId",
request: {
type: "GET",
paramKey: "authorId",
query: {
operator: "==",
},
},
},
]);

The response will be a list of blogs written by the author with the specified authorId.