The Javascript driver allows me to search MongoDB's vast database of 500 million documents using regular expressions on two specific fields, without relying on a predefined schema

My MongoDB database contains approximately 500 million documents structured like this:

{
  "_id": objectId,
  "Name": "John Smith",
  "Address": "132, My Street, Kingston, New York 12401"
}

I am looking to query the data based on both the Name Regex and Address Regex. The issue is that sometimes the name and address are case-sensitive in the database. I have tried creating separate indexes for each field, as well as a compound index with text (but it only searches properly if the text index is on one field - either Address or Name).

I also attempted to create an index for both fields like so: createIndex({Name: 1, Address: 1}); This approach returns data even if I search using just one field. What I want is to be able to perform a query like:

var name = "john smith";
var address = "new york";

 var userData = await db
      .collection("userData")
      .find({
        $and: [
          { Name: { $regex: new RegExp(name, "i") } },
          { Address: { $regex: new RegExp(address, "i") } },
        ],
      })
      .skip(skip)
      .limit(limitNumber)
      .toArray();

When using a single index, I can still query with one field but not with both simultaneously. Trying all the above methods with both fields causes extreme loading times, even after trying to optimize the query execution time. It doesn't return any results or errors.

https://i.sstatic.net/OlmBCcr1.png

Thanks

Answer №1

Here is a different approach:

let userInformation = await db
  .collection("userInformation")
  .find({
      Name: { $regex: name, $options: "i" },
      Address: { $regex: address, $options: "i" } 
  })

Using the $and operator is unnecessary in this scenario.

If you insist on using it, then ensure that it is paired with the $expr function like so:

let userInformation = await db
  .collection("userInformation")
  .find({ $expr: {
    $and: [
      { $regexMatch: { input: "$Name", regex: name, options: "i" } },
      { $regexMatch: { input: "$Address", regex: address, options: "i" } }
    ],
  }})

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

When updating the location.hash property via a click event, it appears to be set to

Check out the code snippet below: $(e).click(function() { console.log(this.href); location.hash = this.href; }); In this code snippet, e refers to a <li> element structured like this: <li href="#about">About</li> The location. ...

The md-select search filter currently functions according to the ng-value, but it is important for it to also

I am using a md select search filter with multiple options available. For instance: id: 133, label:'Route1' id: 144, label:'Route2' id: 155, label:'Route3' id: 166, label:'Route4' If I input '1' ...

The AJAX response consistently returns a 405 status code

I am experiencing an issue with an AJAX post request. $.ajax({ type: "POST", contentType: "application/json", url: "/rating/save", data: JSON.stringify(rating), dataType: "json", mimeType: "application/json" ...

When attempting to retrieve or submit data, an error message may appear similar to this: "Encountered a MongooseError: The operation `users.insertOne()` has timed out after 10000 milliseconds

I recently undertook a project to store employee information, and successfully connected to the database. However, I encountered an error when attempting to retrieve or create a new record: Even after making changes to the MongoDB network settings, the er ...

"Encountered a roadblock while attempting to utilize the applyMatrix

I am currently working on running an animation using a JSON file and a custom JSON loader, not the one provided with three.js. Within the JSON file, there is an object called frames that contains multiple frames, each with shape information and a simulatio ...

Exploring the jQuery Solution for Enhancing Dojo Widget Framework

My experience with Dojo in the past has been great, especially with its widget infrastructure. Being able to easily separate code and HTML content, having a seamless connection with the require system used by Dojo, and the convenient builder that compresse ...

Problem with roles assigned through reactions on Discord

I've been working on a discord bot reaction roles command and everything seems to be going smoothly, except for one issue that I'm facing. After booting up the bot and running the command to create the embed, everything works fine. However, when ...

Utilizing VueJs (Quasar) to access vuex store within the router

Recently, I have been diving into learning Vuex and creating an authentication module. Following a tutorial, I reached a point where I needed to use the store in the router. However, after importing my store at the top of the router index.js file, I notice ...

Utilizing repl.it for a database in Discord.js

I've created a database script on repl.it and it seems to be functioning properly. However, I keep encountering null values in the users' database. Here's my code snippet: client.on("message", async (message) => { if (messag ...

The promise was rejected and paused due to an exception, but no error was recorded

Check out this Node.js code snippet. I am using mongoose to query a MongoDB database. The code gets stuck at "Line A" with a message saying "Paused on Exception". There are no errors shown in the console. Strangely, this only happens when I run the code in ...

Create dynamic HTML content in Angular using the ng-repeat directive

Can anyone help me figure out how to generate dynamic html content using ng-repeat with multiple ng-model instances stored in an array? I keep encountering a syntax error for {{ within ng-model. Is there a way to work around this issue? <div class="c ...

Choosing a table cell in a React Bootstrap table

export class UsersTable extends React.Component { constructor() { super(); this.state = { data: null }; } componentWillMount() { fetch("http://localhost:8081/users/getData") .then(res => res.json()) . ...

Learn how to collapse a list by clicking outside of it on the document with the following code: $(document).on("click"

I want to create a collapsible/expandable menu for my website. I had a version where hovering over a category would expand the subcategory, but what I really need is for the subcategories to expand when I click on a category and remain expanded until I cli ...

What causes a "UnhandledPromiseRejectionWarning" while using Puppeteer?

What causes the following warnings to appear, and how can I resolve them? Warnings: (node:26771) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Protocol error (Runtime.callFunctionOn): Target closed. (node:26771) ...

What is the best way to add an element while preserving its original placement?

I need a solution to duplicate the code of an element and transfer it to another element without removing it from its original position. Currently, I have a JavaScript function that allows you to move a picture to a box when it's clicked. However, th ...

Guide to using get() and res.sendFile() function to redirect webpages

Why is the page not redirecting properly? In my index.html file, I have this script: $.get( "/loginPage", function( data ) {}); The purpose of this script is to check if a user is logged in. If they are, it should redirect them to the lobbyPage. This is ...

There is no 'Access-Control-Allow-Origin' header found on the requested resource in Heroku for Node.js

Here is the header setup in my Node.js API app: res.header("Access-Control-Allow-Origin", "*"); res.header( "Access-Control-Allow-Headers", "Origin, X-Requested, Content-Type, Accept Authorization" ); ...

How to verify the presence of a value within a nested array in MongoDB

In one of my database collections, there is a field that consists of three arrays structured like this: use_homepage: { home: [Array], hidden: [Array], archive: [Array] } This particular field represents the homepage configuration for a user. ...

Webpack-hot-middleware increases the number of event listeners exponentially

When configuring my new development environment (node server + client with vanilla js), I encountered an issue with webpack-hot-middleware for live reloading front-end changes. The problem arose when using code like: $button.addEventListener('click&a ...

Using Angular's $post method to communicate with PHP CodeIgniter for making requests and handling responses

I am having trouble sending data from Angular to Codeigniter using $post. Here is the JavaScript code I am using: $scope.user.first_name = 'first name'; $scope.user.last_name = 'last name'; $http({ method: 'POST', ...