Updating IP addresses in MongoDB

I am dealing with a nested schema in my mongoDB collection. Here's an example of how it looks:

{
   "_id":"61d99bf5544f4822bd963bda0a9c213b",
   "execution": {
        "test_split":0,
        "artifacts":{
            "9ed39_output": {
                "uri": "http://100.com/somefile"
            },
            "8d777_output":{
                "uri": "http://100.com/anotherfile"
            }
        }
    }
}

Each key under "artifacts" is unique. I am looking to replace the IP address stored in the uri field (in this case, "100") with another IP address (let's say "200"). I need to implement a solution using find and foreach. However, I am finding it challenging due to the variable keys under "artifacts". Any guidance on how to achieve this would be greatly appreciated. Thank you.

This database is integrated with ClearML. There is a method for updating model locations as shown here: . I have attempted to adapt this approach to my current scenario, but haven't been successful yet.

Answer №1

It's not entirely clear how you want to modify the document in a general sense. However, you can achieve it by following this approach:

db.collection.aggregate([
   { $set: { "execution.artifacts": { $objectToArray: "$execution.artifacts" } } },
   {
      $set: {
         "execution.artifacts": {
            $map: {
               input: "$execution.artifacts",
               in: {
                  k: "$$this.k",
                  v: {
                     uri: {
                        $replaceOne: {
                           input: "$$this.v.uri",
                           find: "100.",
                           replacement: "200."
                        }
                     }
                  }
               }
            }
         }
      }
   },
   { $set: { "execution.artifacts": { $arrayToObject: "$execution.artifacts" } } },
])

View the Mongo Playground example here

You may need to further enhance the $replaceOne section as necessary. You could consider using regular expressions or utilizing $split and $reduce methods for more complex string manipulations.

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

How can I create a reverse animation using CSS?

Is there a way to reverse the animation of the circular notification in the code provided? I want the circle to move forward, covering up the info and fading out. I've tried switching the animation around but haven't had success. Any suggestions? ...

What is the best way to send the value from a textbox to this script?

My challenge is with this particular textbox: <input type="text" autocomplete="off" required="required" id="bar" name="bar" class="form-control" placeholder="Barcode"> Also, there's a button in the mix: <button type="button" style="float:r ...

needed a bean of class 'com.supriya.banking.repository.AccountRepository' but could not locate one

APPLICATION FAILED TO START Description: Field accountRepository in com.supriya.banking.service.AccountService required a bean of type 'com.supriya.banking.repository.AccountRepository' that could not be found. The injection point has the foll ...

What is the most effective way to choose and give focus to an input using JavaScript or jQuery?

How do you use JavaScript or jQuery to focus on and select an input? This is the relevant snippet of my code: <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> </he ...

Manage image placement using CSS object-position

I have the following code snippet: img{ width: 100%; height: 1000px; object-fit: cover; object-position: left; } <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

What is the best way to send a form using jQuery's AJAX function?

Essentially, I have a form that contains several text boxes along with a submit button. The issue I am facing is that upon submitting the form, only the value of the username box is being sent and not the values of the other text boxes. I am using a servl ...

How can we send state updates directly to a conditionally rendered React component?

I am currently developing a React application with a tab section that displays specific components upon clicking on a tab. Initially, I have my parent component: class Interface extends Component { constructor(props) { super(props); ...

What is the best method for sending a DbGeography parameter to MVC?

I am working on developing an API that allows users to save polygons on the server using ASP.NET MVC 5. Can anyone guide me on how to properly format the AJAX parameters for posting requests with DbGeography? This is what I have tried so far: $.ajax({ ...

Issue with Vue 2 emitting events and not properly executing associated method, despite correct setup

I am attempting to trigger an event from a child Vue component to its parent using this.$emit('collapsemenu'). However, when I try to capture this event in the parent using v-on:collapsemenu="collapseMenuf($event)", nothing seems to ha ...

Vue allows you to easily generate child div elements within a parent

Having some issues creating a child div with Vue. The code is being placed correctly, but it's being stored as an array. <template> <div class="containers" v-bind:style="{ backgroundColor: pageStyle.backgroundColor, paddingLeft:'5%& ...

Struggling with passing the decoded user ID from Node Express() middleware to a route can be problematic

I have encountered a similar issue to one previously asked on Stack Overflow (NodeJS Express Router, pass decoded object between middleware and route?). In my scenario, I am using the VerifyOrdinaryUser function as middleware in the favorites.js route. Th ...

Ways to retrieve a variable within the init() function

My current project involves using datatables along with ajax to display information dynamically. Below is the code snippet I am working with: // Setting up the module var DatatableAdvanced = function() { // Examples of Basic Datatables var _c ...

Obtain the index of a selected option in a Select Tag using Node.js/Express

When you make a POST request with a form in Node.js/Express For example: <select name="selectname"> <option value="value1">Value 1</option> <option value="value2" selected>Value 2</option> <option value="value3"> ...

Having trouble importing components within my router.js file in VueJS

Trying to work with the vue-router, but encountering difficulty importing components into the router.js. Received a warning: [Vue Router warn]: No match found for location with path "/" In need of assistance as I'm unsure of what mistake I ...

The Console.log() function displays the current state and value of a promise object within the Q library

Whenever I attempt to print a promise object from Q, the result that I receive is as follows: var Q = require('q'); var defaultPromise = new Q(); console.log('defaultPromise', defaultPromise); defaultPromise { state: 'fulfilled& ...

Calculate how frequently an element showcases a particular style

I attempted to tally the occurrences of a specific class on an element, but I keep encountering the error message: $(...)[c].css is not a function Does jQuery have it out for me? Below is the code snippet in question: // hide headings of empty lists le ...

What is the best method to reset values in ngx-bootstrap date picker?

At the moment, it is only accepting the most recently selected values. To see a live demo, click here. ...

"Utilizing JavaScript, you can remove an element by clicking on the outer element within the

I need the functionality where, when a user clicks on an input type="checkbox", a corresponding textarea is displayed. Then, if the user clicks anywhere except for that specific textarea, it should be hidden. Can someone assist me with implementing this fe ...

Creating JavaScript code using PHP

In my current project, there is a significant amount of JavaScript involved. I'm finding that simply generating basic strings and enclosing them within "<script>" tags may not be the most efficient approach. What are some alternative methods fo ...

Tips for uploading files in asp.net using an ajax call

I have developed a small asp.net web forms application for managing emails. The interface allows users to input mandatory information such as sender, recipient, and subject. I am now looking to implement file attachments in the emails using the asp.net fil ...