Updating embedded documents with new information in mongodb

After searching on SO, I couldn't find a satisfactory answer to my question related to working with mongodb and php for the past couple of weeks. So here I am seeking help. I have a database where I need to add new data to one of the embedded/nested documents, but I'm struggling to do so.

This is the current structure of my data:

{    
    "_id" : ObjectId("5ab0dc4fb57dce17e0002c5c"),
    "meter" : "meter_2",
    "data" : {
        "magnitude1" : {
            "date" : "05/03/2013 08:13:18",
            "value" : 200
        },
        "magnitude2" : {
            "date" : "30/06/2014 23:46:05",
            "value" : 126
        }
    }
}

Now, I'm attempting to add new data to the subdocument magnitude, like this:

{    
    "_id" : ObjectId("5ab0dc4fb57dce17e0002c5c"),
    "meter" : "meter_2",
    "data" : {
        "magnitude1" : {
            "date" : "05/03/2013 08:13:18",
            "value" : 200,
            "somedata" : "somevalue"
        },
    }
}

I've tried various approaches, including the one below, but none of them seem to work in inserting the new data.

$center->updateOne(
    ['_id' => new \MongoDB\BSON\ObjectID('5ab0dc4fb57dce17e0002c5c'), 'data.magnitude1'],
    ['$set' => ['somedata' => 'somevalue']]
);

If anyone could provide assistance or guidance on this matter, it would be greatly appreciated.

Answer №1

Special thanks to @Veeram for providing the solution to this question. The following code snippet shows the answer which could be beneficial for someone facing a similar issue in the future.

$center->updateOne(
    ['_id' => new \MongoDB\BSON\ObjectID('5ab0dc4fb57dce17e0002c5c')],
    ['$set' => ['data.magnitude1.somedata' => 'somevalue']]
);

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

Transmitting the identification number of a post with the help of J

I am currently working on a while loop in PHP that fetches associated results from a query and displays them within a class identified by the user who created the post. My goal is to send the id of the class to PHP, similar to how I am sending the num vari ...

HTML code displaying a fixed message at the top of the webpage

I'm working on a web page that has a lot of content. I need to have a message always visible at the bottom of the browser window, even when scrolling. Can anyone help me achieve this using simple CSS, HTML, jQuery, or PHP? Thanks! ...

Can you provide a visual representation of the JSON structure?

$data = array('name'=>'John', 'age'=>25, 'city'=>'New York'); I am practicing my PHP skills by using cURL for making a POST request. Assuming the given array is converted into JSON format, what wo ...

Retrieve the specific object from the array filter using Mongoose

My mongodb data structure is set up like this {_id:ObjectId(''), "awards" : [ { "award" : "Ballon d'Or", "numberOfTimes" : 6 ...

Tips for renaming property names during JSON serialization in asp.Net

In my MVC Web API Controller, there is a method that I have exposed: [HttpPost] public JsonResult<CustomClass> Details(RequestClass request) { var encoding = Encoding.GetEncoding("iso-8859-1"); var settings = new ...

Retrieve information from a MySQL database and integrate it into a different application

This php script is used to generate a table with a "book" button next to each row. The goal is to extract the values of "phase" and "site" from the specific row where the "book" button is clicked, and transfer them to another form (in "restricted.php") fo ...

The nodejs events function is being triggered repeatedly

I have been developing a CMS on nodejs which can be found at this link. I have incorporated some event hooks, such as: mpObj.emit('MP:FOOTER', '<center>MPTEST Plugin loaded successfully.</center>'); Whenever I handle this ...

The occurrence of an error is triggered by the Jquery.ajax()

Here's a useful link for you to try out: If you take a closer look, you'll see that it takes the $_GET['fname'] parameter and outputs the content I need. I made an attempt at solving this: $.ajax({ type: 'GET', url: ...

Having trouble with the lengthy response time of Hibernate's query.list() function?

I have been working on a REST server that deals with JSON data, and integrates Hibernate with Spring to query the database and return JSON responses to the Rest Controller. The client application has a fixed timeout of 5 seconds for reading responses. How ...

Troubleshooting: PHP Integration Issue with HTML Document

I have a simple HTML file displaying text and including a PHP file with basic echos: The HTML code is in 'home.html' file: <html> <body> <h1>Welcome to my home page!</h1> <p>Some text.</p> <p>Some more ...

Python script utilizing curl to make requests to a URL through an API

As a novice Python scripter, I encountered a challenge when trying to integrate a link shortening service. The API documentation provided by the service is in JSON format and requires URL calls using curl. Due to my limited experience, I struggled with i ...

``There seems to be an issue decoding the JSON array

I am currently working on a PHP script to decode JSON data and insert the objects into my database. However, I am encountering an error: Fatal error: Cannot use object of type stdClass as array in phptest.php on line 21 $postdata = file_get_contents("php: ...

I am looking to develop a straightforward login system. When a user decides to register, the following information will be displayed

Sorry, the requested object is not found! The URL you are looking for does not exist on this server. It's possible that the link on the page you came from is incorrect or outdated. Please notify the author of the page about this error. If you believe ...

The controller is unable to accept JSON data from the JQuery.ajax function

I have thoroughly checked my web.xml and spring-servlet.xml files, but I couldn't find any issues. I then reviewed my Controller and .ajax() call, but still couldn't identify the problem. I experimented with using JSON.stringify, @RequestParam, a ...

Every single image within a specific domain

Exploring a way to create a gallery showcasing all the images within my domain's internet root folder. These images are scattered across various folders. What is the most efficient method to navigate through these folders and display the images? ...

How can we focus href on a <div> using CMS in PHP?

I am currently in the process of learning PHP and simultaneously tackling a redesign of a custom CMS. The CMS is written in PHP, and I have already revamped the default menu using CSS to incorporate a side slide effect. However, my next challenge is to en ...

Learn the process of merging queries from two tables and grouping them into a JSON Array on a WCF Service

I have two tables in my SQL Server Database and I would like to combine them into a single JSON Array within my WCF Service. Table1: ---------------- | type | total | ---------------- | A | 2 | | B | 3 | | C | 4 | ---------------- T ...

Converting objects into JSON format

I'm trying to transform an object that looks like this: { user[id]: 1, user[name]: 'Lorem', money: '15.00' } Into the following structure: { user: { id: 1, name: 'Lorem', }, money: ...

Uploading files using Ajax without the need for additional plugins or submitting a form

I have been struggling to find a solution for uploading files via Ajax when the input field is not enclosed within a form tag. I have already attempted this method. This is the HTML snippet: <span id="user-image-up-btn">Upload Image</span> &l ...

The json_decode() function requires a string as its first parameter, but an object was provided instead

I retrieved an output object class from a query that looks like this: $so_result = stdClass#14 { "colorname" => "Silver" , "colorgroupname" => "MEDIUM SILVER" , "colorcode" => "1D4" , "brand" => null }; echo json_decode($so_result, ...