Refine search results based on various attributes for products using Firebase

Recently, I inquired about how to filter products based on their child property. You can find the original question here: Filter products based on the sub child in Firebase.

To summarize, my data structure is as follows:

products/
     product1
        /author: 12345
        /title: "Awesome"
        /category: "catA"
        /description: "more awesome"
     product2
        /author: 67890
        /title: "Other"
        /category: "catB"
        /description: "otherawesome"
     product3
        /author: 12345
        /title: "Billy"
        /category: "catB"
        /description: "otherawesome"

To filter out all products with author 12345, you can use the following query:

ref.child("products").orderByChild('author').equalTo(12345)...

But what if you need to use multiple AND or OR statements?

For example, how do you filter for:

  • all products with author 12345 AND category catA (resulting in product1)

  • all products with author 12345 OR author 67890 (resulting in product1 and product3)

I attempted the following query for the first scenario:

ref.child("products").orderByChild('author').equalTo(12345).orderByChild('category').equalTo('catA')...

Unfortunately, this resulted in an error (

You can't combine multiple orderBy calls
).

For more information, you can refer to: https://www.firebase.com/docs/web/guide/retrieving-data.html#section-queries

Answer №1

After researching extensively, I stumbled upon this insightful article that sheds light on the best approach to handling multiple where clauses in Firebase queries. The suggested method involves executing the initial query (ideally one with minimal results) and then applying JavaScript filters based on additional criteria. Alternatively, you can optimize this process by establishing a compound index for your query. An exemplary demonstration of this technique is provided in Frank van Puffelen's comprehensive explanation highlighted in the linked source.

products/
     product1
        /author: 12345
        /title: "Awesome"
        /category: "catA"
        /description: "more awesome"
        /author_category: "12345_catA"
     product2
        /author: 67890
        /title: "Other"
        /category: "catB"
        /description: "otherawesome"
        /author_category: "67890_catB"
     product3
        /author: 12345
        /title: "Billy"
        /category: "catB"
        /description: "otherawesome"
        /author_category: "12345_catB"

In terms of incorporating an 'OR' condition, my exploration did not yield any definitive solutions. Hence, my recommendation would be to execute two distinct queries to meet your specified criteria.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Improving performance in Next.JS by optimizing unused JavaScript resources

Currently working on my first website using Next.js and experiencing poor performance scores after running a lighthouse test. The issue seems to be related to unused JavaScript files located in the chunk folder. I've come across suggestions to split t ...

Track current database connections at a specific moment

In relation to my inquiry here, I am encountering delays with Time-to-first-byte in my NodeJS application, utilizing Knex as the database library. Versions: Postgres: 9.6.1 Node: 7.4.0 Knex: 0.12.6 Angular: 1.3 It was recommended that: To address this ...

Having trouble initiating npm?

I am currently learning Angular2, and I encountered some issues when trying to initiate the npm server by running npm start in the project's directory. Here are the errors I received: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" ...

Sending a CSS class name to a component using Next.js

I am currently in the process of transitioning from a plain ReactJS project to NextJS and I have a question. One aspect that is confusing me is how to effectively utilize CSS within NextJS. The issue I am facing involves a Button component that receives ...

Determine if a link can be activated or disabled

Controlling the clickability of a link and displaying an error based on the result of an ajax call is my current objective. <a class="lnkCustomer" href="http://localhost/viewcustomer" target="_blank" data-customerno="2 ...

Stopping an HTTP request and handling errors in Angular

Hi, I need help with canceling my HTTP request after 2 seconds. If no data is received within that time frame, I want it to resolve into the error function and return an empty object. I understand that I need to use the timeout property, but I'm not ...

Implementing dynamic watchfile listeners in Node.js upon client connection and appropriately removing them thereafter

Currently, during a client connection event, a file is being watched. However, when a second client connects, the same file is watched again using fs.watchFile(). Upon a client disconnecting event, the file is unwatched using fs.unwatchFile(). This means ...

What is the reason behind Jquery targeting only the initial div element?

I am attempting to use the "replace" method in jQuery to eliminate all non-numeric characters within a div. However, I have found that the replace function in jQuery is only impacting the first element it encounters. Below is the snippet of my jQuery cod ...

AngularJS directive for LocalStorage operations

I rely on ngStorage (accessible at https://github.com/gsklee/ngStorage) to save certain data in the browser's localStorage. Here is the markup I am working with: <ul class="cards"> <li ng-repeat="card in cards"> <card>& ...

Opencart is displaying a resource as a font even though it was transferred with a MIME type of text/html

Despite searching extensively on Stack Overflow, none of the answers I found have been able to resolve my issue. I am using an Opencart Store with custom typography and encountering the following error: Resource interpreted as Font but transferred with MI ...

How to Initialize Multiple Root Components in Angular 4 Using Bootstrap

We are working on a JQuery application and have a requirement to integrate some Angular 4 modules. To achieve this, we are manually bootstrapping an Angular app. However, we are facing an issue where all the Angular components are loading simultaneously wh ...

Having an issue with the isClicked() function in SYMFONY FORM when combined with JavaScript - jQuery: event.preventDefault() and event.target.submit(), depending on the browser used

My Objective: I am aiming to incorporate two submit buttons on a Symfony FORM. The first button will be used for form validation, while the second submit button will serve as an exit option from the form. By default, the form fields have the required att ...

Encoding a two-dimensional array into JSON format

In my PHP script, I am querying a database and formatting the results as JSON: $query = mysql_query($sql); $rows = mysql_num_rows($query); $data['course_num']=$rows; $data['course_data'] = array(); while ($fetch = mysql_fetch_assoc($q ...

What does it mean to have the "onload" event in the <body> tag of HTML and JavaScript?

I'm curious to know if "onload" is an attribute of the <body> tag in HTML. I came across this code snippet while reading a book on JavaScript. <html> <body onload="alert('hi')";> ... </body> </html&g ...

How to Retrieve URL Parameters in Gatsby

As I delve into learning React and Gatsby, I encountered an issue with my page containing URL parameters. Despite everything working smoothly on my local machine, after the gatsby build process, the variable url = undefined. How can I retrieve these para ...

Tips for aligning the TextBox in the center of an Asp.net website

I have the following HTML/ASP code that I need to center the TextBox on the display page. Here is my code snippet below. Ideally, I would like to have the label displayed next to the TextBox once it is centered. <center> <div class="container ...

Steps for accessing the "this" keyword within the parent function

Struggling with referencing `this` within a parent function while building a basic tab system using AngularJS. It seems like I may not have a solid grasp on the fundamentals, so any guidance would be appreciated: JavaScript: $scope.tabs = { _this: th ...

Using React and Material UI to create a table filtering system with checkboxes

I'm currently developing a filtering feature using checkboxes for a data display in a project utilizing React and Material-UI. Is there a custom solution available within Material-UI? If not, what steps should I take to achieve this? As I am rel ...

Tips for maintaining the current page after refreshing in AngularJS

Hello, I am new to angularjs and recently created a login page for my project. However, I encountered an issue where, after logging in successfully, the page redirects me to the home page. But when I click on the refresh button, instead of staying on the ...

Identify specific terms within a webpage using an iframe that is integrated onto the same page

Is there a way to implement word highlighting on a webpage containing an iframe with a search field? The concept involves allowing a user to input search terms within the iframe, which would then send a command to highlight those words on the main page. ...