Retrieve all the records from the collection that have a specific reference number in their ID field

Is it feasible to pull together all documents with an ID that includes a specific value? I am working with Angular 7.

I attempted using db.collection('items').where.. but unfortunately, this method is not supported.

For instance:

(collection)
/items
     (doc)
     /green apple
     /red apple
     /banana
     /melon

Is there a way to query the 'items' collection in order to fetch all documents where the ID has 'apple'? Any help would be greatly appreciated as I have been unable to find a suitable solution.

Answer №1

To enhance the search functionality, consider adding a new property called searchTerms when creating or updating a document. Here's how you can achieve this:

let docName = 'blueberry pie';
let ref = /* some path */.collection('items').doc();  
let doc = { /* initialize your doc here */ }

doc.searchTerms = docName.split(' ').reduce((acc, term) => {
      acc[term] = true;
      return acc;
    }, {});

ref.set(doc)

When querying for these docs later, use the following approach:

let targetTerm = 'pie'
let key = `searchTerms.${targetTerm}`;
let query = /* some path */.collection('items').where(key, '==', true)

In a recent update in August, the platform added support for searching arrays in Firestore. This allows you to simplify the code by using where-contains in the query:

// same as before
doc.searchTerms = docName.split(' ');
ref.set(doc)

For future implementation, you can create a cloud trigger with onCreate to automate the process of building the search terms field. This way, developers working on document creation don't have to worry about this aspect.

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

Customize the text displayed on the select box button in a React application

My task involves working with an array of JSON objects structured like this - [ { "country_code": "AF", "country_name": "Afghanistan", "country_flag": " ...

Error occurs in ReactJS App when the state is updated in Redux only after dispatching the same action twice

Check out the following code snippet: ChartActions.js import * as types from './ChartTypes.js'; export function chartData(check){ return { type: types.CHART_DATA,check }; } ChartTypes.js export const CHART_DATA = 'CHART_DATA ...

Communication breakdown between components in Angular is causing data to not be successfully transmitted

I've been attempting to transfer data between components using the @Input method. Strangely, there are no errors in the console, but the component I'm trying to pass the data from content to header, which is the Header component, isn't displ ...

Having trouble sending a FormData object with Axios for a user uploaded file in a React, Node.JS project

My goal is to enable users to upload images to my application, which consists of a React frontend and a Node backend. The process involves sending the uploaded file from the client to the server, followed by making a request from the server to Firebase clo ...

Unable to attach an onClick event handler to <TableRowColumn> element using Material-UI in React

In the past, I had a feature that allowed me to change the color of the text from red to green by clicking on a table cell. After introducing Material-UI in my React app and replacing the <td> tags with <TableRowColumn> tags, I noticed that th ...

Toggle the visibility of images with input radio buttons

Explanation I am attempting to display an image and hide the others based on a radio input selection. It works without using label, but when I add label, it does not work properly. The issue may be with eq($(this).index()) as it ends up selecting a differ ...

Transform the features of a website into a sleek iOS application using Swift

Can I take an HTML website coded with JavaScript and integrate its functionality into my app using WebKit or another method? By using WebKit or WebViews, I can load an entire webpage into my app, automatically bringing along its functionality. However, i ...

What is the best way to determine whether a YouTube video permits embedded playback?

When working with user-generated content, it's important to note that YouTube channels may restrict their videos from being played in an embedded player. In order to provide a better user experience, I need to detect the specific reason why a video ca ...

Tips for receiving responses when data is posted to a URL in Node.js using `req.body.url`

How can I retrieve data from the URL provided in req.body.url using Postman's collection tool? In my code snippet, I have defined a key as "URL" and the corresponding value as a live URL. You can refer to the screenshot below for more details: const ...

What is the best approach to animating a specified quantity of divs with CSS and javascript?

How neat is this code snippet: <div class="container"> <div class="box fade-in one"> look at me fade in </div> <div class="box fade-in two"> Oh hi! i can fade too! </div> <div class="box fade-in three"& ...

Performing an AngularJS $http POST request with a combination of JSON parameter and query string

I'm currently trying to write an AJAX call using Angular's $http object in order to send a post request with JSON in the body and a query string appended to the URL. Despite going through the documentation for $http, looking for solutions on SO, ...

Retrieve an array of specific column values from an array of objects using Typescript

How can I extract an array from an array of objects? Data var result1 = [ {id:1, name:'Sandra', type:'user', username:'sandra'}, {id:2, name:'John', type:'admin', username:'johnny2'}, ...

When the component is initialized, the computed property is not being evaluated

My maps component initializes a Google map, adds markers based on props passed from the parent, and sets the correct bounds of the map. However, the markers are added through a computed property to make it reactive. Everything seems to be working fine, exc ...

Refresh a particular element using javascript

I'm new to Javascript and I am trying to refresh a specific element (a div) using only Javascript when that div is clicked. I came across the location.reload() function, but it reloads the entire page which doesn't fit my requirements. Upon clic ...

Transfer a term to a different division - JavaScript Object Model

I am trying to achieve a specific task where I need to move one term under another in SharePoint. However, despite my efforts using JSOM and SharePoint 2013, I have not been able to find a direct method for moving terms. The code snippet I have used below ...

Retrieving the value from a concealed checkbox

I have been searching everywhere, but I can't seem to find a solution to this particular issue. There is a hidden checkbox in my field that serves as an identifier for the type of item added dynamically. Here's how I've set it up: <inpu ...

Randomly undefined custom jQuery function

Appreciate your assistance in advance. I am facing a peculiar scope issue on a page where multiple charts are rendered intermittently. I have created a set of jQuery functions to display different types of charts based on the provided options. Each funct ...

Need a hand with ajax?

Recently, I've been having issues with a PHP script that packages files into a zip based on user input. Unfortunately, the server occasionally errors out and causes all the form data to be lost. To prevent this from happening in the future, I was info ...

Webpack has made Rails .js.erb templates obsolete

Recently, I migrated my Rails application to use WebPack for handling assets, and it has been operating smoothly. However, I encountered an issue with JS templates located in my views directory (*.js.erb) that require jQuery. Since jQuery is included in my ...

The jQuery show/hide functionality is malfunctioning

In my PHP code, I have the following loop: foreach ($sid as $key => $value) { $sql = " a sql query "; $vehicle->rowQuery($sql); $numRows = $vehicle->rows; while ( $data = $vehicle->result->fetch_assoc()) { $ ...