The query designed to conduct a thorough search using regex is experiencing some issues and not performing as expected

How can I perform an in-depth regex search in MongoDB using a JavaScript function within the query? Here's an example:

db.collection.find(
        {$where: function() {
        function deepIterate(obj, value) {
        var new_value = new RegExp("^"+value, "i");
        for (var field in obj) {
            if (obj[field] == new_value){
                return true;
            }
            var found = false;
            if (typeof obj[field] === 'object') {
                found = deepIterate(obj[field], new_value)
                if (found) { return true; }
            }
        }
        return false;
    };
    return deepIterate(this, "[A-Ba-b0-9]*60.51[A-Ba-b0-9]*")
}}
)

However, the current query is not returning any values and outputting Fetched 0 record(s) in 4ms. Despite having a record in the database with the string 192.108.60.51, it is not being returned. Any help on this issue would be greatly appreciated!

Answer №1

To transform your regular expression pattern, utilize

deepIterate(this, "(([1-9]?\\d|1\\d\\d|2[0-5][0-5]|2[0-4]\\d)\\.){3}([1-9]?\\d|1\\d\\d|2[0-5][0-5]|2[0-4]\\d)$")
. This regex is specifically designed to identify strings that represent IP addresses. The lack of matches in your database is likely due to a mismatch between the pattern and the records.

Answer №2

Discovered the solution:

db.collection.find({$where: function() {
        function  recursiveSearch(obj, value) {
        regex_value = new RegExp(value, 'i');
        for (var field in obj) {
            if (regex_value.test(obj[field])){
                return true;
            }
            var found = false;
            if ( typeof obj[field] === 'object' ) {
                found = recursiveSearch(obj[field], regex_value)
                if (found) { return true; }
            }
        }
        return false;
    };
    return recursiveSearch(this, "[A-Ba-b0-9]*60.51[A-Ba-b0-9]*")
}})

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

jQuery loading and updating

I am faced with a scenario where I have both a 'test.html' and a 'test.php' files. The content of 'test.html' is as follows: <html> <head> <script type="text/javascript" src="/js/jquery-1.8.2.js">< ...

Stop a loop that includes an asynchronous AJAX function

Embarking on my Javascript journey as a beginner, I find myself facing the first of many questions ahead! Here is the task at hand: I have a directory filled with several .txt files named art1.txt, art2.txt, and so on (the total count may vary, which is ...

Refresh the DOM based on changes in Vuex store state

One of the issues I'm facing is with an 'Add To Basket' button that triggers a Vuex store action: <button @click="addToBasket(item)" > Add To Basket </button> The Vuex store functionality looks like this: const actions = { ...

emptyQueue in jQuery

I'm currently working with a jQuery script that takes the src of an image, places it in a hidden div, and enlarges the image with an animation when hovering over the requested image. However, I've encountered an issue where the clearQueue or stop ...

What is the technique for hiding the bottom tab navigator upon leaving a specific screen in React Native version 5?

In the home screen, I want only the bottom tab navigator to be visible, and then hidden until the user returns to the home screen. The example provided below is tailored for working in the App.js file, but my situation is different. const Tab = createBot ...

Encountering an issue with usememo in React js?

I'm currently experimenting with the useMemo hook in React JS. The goal is to sort an array of strings within a function. However, when I return the array from the function, only the first element is being returned. Can someone please assist me in ide ...

Vuetify's data table now displays the previous and next page buttons on the left side if the items-per-page option is hidden

I need help hiding the items-per-page choices in a table without affecting the next/previous functionality. To achieve this, I've set the following props: :footer-props="{ 'items-per-page-options':[10], &apo ...

What is preventing the table from extending to the full 100% width?

Displayed above is an image showing an accordion on the left side and content within a table on the right side. I have a concern regarding the width of the content part (right side) as to why the table is not occupying 100% width while the heading at the ...

Ways to address the Uncaught TypeError: Cannot read property 'field' Tabulator error

My Current Task: I have Tabulator table data serialized and saved in my database. At this point in my script, I am retrieving and parsing the data. My goal is to organize the column data into an array to populate my Tabulator table, taking into account col ...

Facing issues connecting to my MongoDB database as I keep encountering the error message "Server Selection Timed Out After 3000ms" on MongoDB Compass

I am encountering an error on my terminal that says: { message: 'connect ECONNREFUSED 127.0.0.1:27017', name: 'MongooseServerSelectionError', reason: TopologyDescription { type: 'Single', setName: null, maxS ...

Guide on dynamically changing the color of polymer 1.0 paper-button from an array of colors

I have an array called buttonColors that contains a set of colors in hex format. I am looking to create paper-buttons, each with the color from the buttonColors array. How can I achieve this in Polymer 1.0? <template is="dom-repeat" items="{{buttonCo ...

nodemon is launching an endless number of .node-xmlhttprequest-sync files

I am facing a strange issue with my app that imports a module containing a promise. Everything runs smoothly when I start the app using "node app.js" However, if I use "nodemon" to launch it, it constantly creates files with names like .node-xmlhttpreque ...

Issues with Navigating through a Scrollable Menu

I'm having a difficult time grasping the concept and implementing a functional scrolling mechanism. Objective: Develop a large image viewer/gallery where users can navigate through images by clicking arrow keys or thumbnails in a menu. The gallery an ...

What is the best way to prevent a draggable div from moving past the edge of the viewport?

My situation involves a draggable div that functions as a popup window when a specific button is clicked. However, I noticed that dragging the div to the end of the viewport allows me to drag it out of the visible area, causing the body of the page to expa ...

Having trouble with the setHours function in React not behaving as anticipated?

I'm having trouble adjusting the time in my React application using the setHours method: function App() { let currHour = new Date().setHours(15); return ( <div> <h1>{currHour}</h1> </div> ); } Inst ...

How can I silence the warnings about "defaultProps will be removed"?

I currently have a next.js codebase that is experiencing various bugs that require attention. The console is currently displaying the following warnings: Warning: ArrowLeftInline: Support for defaultProps will be removed from function components in a futur ...

What is the best way to incorporate variables into a text file using ajax?

I am having an issue with my code. I have written a script that allows users to draw an image on canvas and then extract pixel values from the clicked location, writing them into a text file using ajax. However, I am facing problems with my onclick and a ...

Add a luminous shine to a THREE.TextGeometry element within the Three.js environment

After conducting extensive research on Google, I am still trying to determine if it is possible to achieve a glow effect within the three.js environment. My goal is to create a glowing effect based on TextGeometry rather than a simple primitive shape like ...

What is the best way to designate multiple fields as primary key in MongoDB?

I have a challenge in front of me – creating a collection with over 50 fields. As far as I know, the primary key's function is to uniquely identify a record. Given that the primary key is the _id in MongoDB and is automatically generated, does it me ...

Enhancing JSON Objects in AngularJS with Custom Properties and Calculations

Hello, I'm still getting the hang of angularjs and could use some guidance. I have a Rest service that provides data on saleItems when a get request is made, as shown below: saleItems = [ { "id": 236, "variant": "Oval Holder", "mrp": "6 ...