Retrieve all documents from PouchDB that match a specific regex pattern by their IDs

Is there a way to retrieve all documents with IDs that match a specific regex expression?

Let's say we have the following document IDs:

p0
p0/e0
p1
p1/e0

How do we only retrieve p0 and p1? Using the regex /^p[0-9]+$/.

Currently, it takes two requests to achieve this. Is there a method to accomplish this with just one request?

This.db.allDocs({
    include_docs: false
}).then(function(result){

    // Find all IDs that match /^p[0-9]+$/
    var iDoc = result.rows.length;
    while(iDoc--){
        if(result.rows[iDoc].id.match(/^p[0-9]+$/)){
            projectsIds.push(result.rows[iDoc].id);
        }
    }

    // Retrieve all documents with IDs matching /^p[0-9]+$/
    This.db.allDocs({
        include_docs: true,
        keys: projectsIds
    }).then(function(result) {
        var iProject = result.rows.length;
        var docs = [];
        while (iProject--) {
            docs[iProject] = result.rows[iProject].doc;
        }
        projects.resolve(docs);

    });

});

Answer №2

To retrieve all of your documents, utilize the allDocs() function and subsequently apply JavaScript filtering in memory.

This approach entails loading your entire database into memory, as PouchDB is unable to index using a regex pattern. Therefore, this method is necessary! Alternatively, you can structure your document IDs in a way that facilitates prefix searching, as suggested by another user.

Answer №3

It seems that a solution may have been found with the pouchdb-find plugin (credited to @nlawson). The Pouch documentation provides insight on how to query with this plugin...

Here's an example of how it can be implemented:

async test()
  let result = await This.db.find({
    selector: {_id: {$regex: '^p[0-9]+$'}}
  });
  let docs = result.rows.map( row => row.doc )
  console.dir(docs)
}

(I made it asynchronous so you won't need to deal with callbacks)

You can experiment with it yourself on the pouchdb-find plugin's website.

However, performance might not be optimal as suggested by @nlawson, who believes that executing a regex on an index could be inefficient.

UPDATE: Upon reviewing the mentioned Pouch documentation, it appears that indexes are indeed utilized in the find process, potentially leading to better performance.

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

Struggling to organize data into a hierarchy using the D3.js nest function

Attempting to build a d3.js tree, I need my data to be in hierarchical form before using d3.treeLayout. Currently, here is what my json array looks like: var data = [ {"group":"1","price":"36599.88", "cy":"44260"}, {"group":"2","price":"36599 ...

Implementing iOS scrollbars as a solution for the Bootstrap 4 Carousel Bug that triggers extra swipes with just a click or tap

The issue & environment Utilizing Bootstrap 4 via CDN, a problematic scenario has arisen with the Carousel feature on iOS devices. An identified bug persists in this aspect of Bootstrap, acknowledged with a potential resolution available through githu ...

Guide on building a library of React Native components

Can you provide guidance on building a React Native component library? I am looking to incorporate it with this command: npm install ...

The $.ajax POST method successfully sends data to the server, but the $.ajax PUT method fails to do so

When utilizing jQuery to make an ajax call, I observed that using the POST method works perfectly fine. However, when switching to the PUT method without any other alterations, the object data is not being sent. This has led me to the question of why this ...

Show all <p> lines in a .txt file in a horizontal format instead of vertical using javascript and jquery

I'm attempting to export the content from a div as a .txt file, but all the paragraphs are displaying side by side. For instance, within the div content, I have the following lines: <div id="content> <p>hello</p> <p>H ...

Integrating image transitions into JavaScript

Could you provide guidance on how to create buttons from the three images within the "fadein" div in order to navigate directly to a specific jpeg? Currently, the three jpgs are displayed in a continuous loop. Thank you. ...

Creating a Dual Y-Axis Chart with Two Sets of Data in chart.js

I utilized the chart.js library to write the following code snippet that generated the output shown below. My primary concern is how to effectively manage the labels on the horizontal axis in this scenario. CODE <!DOCTYPE html> <html lang="en"& ...

Is it possible to add information to the form data object?

When I send an image to the server using form data, everything seems fine until I try to add data from a crop plugin. Here is the code snippet: //create form data from form var formData = new FormData($('#my-form')[0]); //append the image formD ...

Creating a personalized HTTP service in Ionic 2 rc0

I am currently in the process of migrating from an ES6 project (beta11) to a new project using rc0, and I have encountered some challenges while trying to refactor a custom http service that I had previously. Within my providers/ directory, I have a file ...

What is the best approach to defining a type for a subclass (such as React.Component) in typescript?

Can someone help me with writing a type definition for react-highlight (class Highlightable)? I want to extend Highlightable and add custom functionality. The original Highlightable JS-class is a subclass of React.Component, so all the methods of React.Com ...

Obtaining a Jquery object for a particular class with a specific value

I'm looking to retrieve a specific Jquery object with a particular class and value. Let's say I have these input tags with the class "track-interval". <input type="radio" class="track-interval" value="1" name="interval" /> <input type= ...

Replace Picture as you Scroll

Is it possible to swap images based on scroll position? For instance: When scrolling 100px, display img1.jpg When scrolling 200px, display img2.jpg When scrolling 300px, display img3.jpg Here's an example of what I'm looking for. Any suggest ...

Encountering issues while retrieving data from a JSON object

Having trouble retrieving information from a JSON object using JavaScript and jQuery. I keep receiving an exception indicating that "code" is not set. My goal is to extract a country code from a string and automatically populate an input field based on the ...

Assistance with the JQuery Validation Plugin

Currently, I am utilizing the JQuery validation plugin to validate a form. However, in addition to displaying an error message, I also want it to modify the CSS for the td element above it. This is my current implementation: function handleValidationError ...

Iterate through elements within an HTML table by utilizing jQuery

I have a table. My goal is to loop through all the items in the table and extract their quality and price data. However, I'm struggling to access these values efficiently. Here is the structure of the table: <div class="table-items__container"&g ...

An error occurred - 0x800a1391 - JavaScript runtime error: The function 'SelectAllCheckBoxes' has not been defined

I'm currently in the process of learning web development and I am trying to incorporate jQuery into my ASP .NET page. Within the header section, I have included the necessary references: <head id="Head1" runat="server"> <link href=" ...

SQLite Simplified - A Primer on Fundamentals

I'm currently experimenting with the SQLike query engine from Thomas Frank's website and finding it difficult to grasp the basic concept. In my project, I have JSON data sourced from my PHP code, which is structured like this: var placesJSON=&l ...

Utilize Jquery to toggle text input forms based on radio button selections

I have a group of text inputs labeled with the class 'bankinput'. Alongside them, I have two radio buttons generated by asp.net helpers. The ids assigned to these radio buttons in the generated html are 'Cheque' and 'Draft'. F ...

The table in Firefox is not displaying and hiding correctly. It only shows the header when the button is pressed, but not the entire row

There seems to be an issue with Firefox when it comes to displaying and hiding my table. When the button is pressed, only the header appears but not the row. What I'm trying to achieve is that when the user clicks the button, both the column header w ...

Remaining stationary as additional elements are inserted at the start of the div

I'm currently working on a chat page. Within this page, there is a div element that serves as the message container. As users scroll up, a request is sent to the server to retrieve older messages, which are then added to the beginning of the message c ...