What is the best way to add values from a nested array to a MongoDB document?

In the mongodb document I have, it is structured like so:

{
    "_id": "user1",
    "loc": [
        {
            "lon": 51.12076493195686,
            "lat": -113.98040771484375
        },
        {
            "lon": 51.10682735591432,
            "lat": -114.11773681640625
        }
    ]
}

I am wondering how to add a new array of lon and lat within the loc list?

The code snippet I tried was:

db.collection('location').update({_id:'user1'},{'$push': {"lat": "-107.10400390625", "lon": "33.32343323432" }})

However, this approach doesn't work because lat and lon are actually nested within loc.

Answer №1

db.collection('location').update({_id:'user1'},{'$push': { "loc": {"lat": "-107.10400390625", "lon": "33.32343323432" }}})

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

Having trouble with my @font-face not displaying in Firefox, can't seem to pinpoint the issue. Any assistance would be greatly appreciated

I have encountered a problem with the code displayed below. It seems to work perfectly fine on Google Chrome and Internet Explorer, but when I try to load it on Firefox, the custom font fails to load. Can anyone offer some advice on how to resolve this iss ...

Ensure that the promise is fulfilled only if a specific condition is met

I have a complex if-else condition in my code that includes different promises. Once the logic determines which condition to enter and executes the corresponding promise, I need to ensure that a final promise is always executed. if (a < 5) { vm.pr ...

"Integrating a typeface.json font file into your three.js project: A step-by

I'm looking to incorporate 3D text into my website using the following code (reference: Labelling the vertices in AxisHelper of THREE.js): var textGeo = new THREE.TextGeometry('Test', { size: 10, height: 5, ...

The function 'find' cannot be invoked on an undefined object

I'm currently working on implementing objects in my jQuery code. So far, I have the following: var options = { ul: $(this).find('.carousel'), li: options.ul.find('li') } The li property is causing an error - Cannot call meth ...

Automatically trigger a Bootstrap 5.2 modal when the page loads

Currently, I've been utilizing Bootstrap in conjunction with JQuery and have a specific code snippet that displays a Modal when a page is loaded. However, with the latest version 5.2 of Bootstrap, there is a push to move away from using JQuery (which ...

Radio button fails to be marked as selected

Currently, I am conducting a test on , and one of the tasks involves styling Radio Buttons using JavaScript (jQuery). Despite my repeated attempts to reconstruct the code, I always encounter the same issue: although JS successfully sets the input radio but ...

The use of JQuery repeating fields can cause disruptions to the Bootstrap layout when removing rows

I have been struggling with a form that contains multiple fields that need to be repetitive. My current code is functional, but I'm encountering an issue where clicking on any remove button other than the first one causes the fields in the row to rear ...

Utilize JavaScript to eliminate tags surrounding a text node

If my HTML code looks something like this: <div id="text"> This is some text that has an area <span class="highlight">highlighted with different formatting</span> and then some more text. </div> I am trying to figure ...

How can jQuery distinguish between implicit and explicit CSS height values?

When working with jQuery, I have noticed that both $('#foo').height() and $('#foo').css('height') will return a value regardless of whether a height property is explicitly set using CSS. Is there a way to determine if an eleme ...

Issue with IE7 when using JQuery to repopulate a <ul> unordered list: new elements showing up under previously hidden elements

Within this javascript snippet, I am utilizing the code below to clear out a list of countries within a <ul> element and then repopulate it (with a slight animation using jQuery's hide() function). The functionality works smoothly in Chrome and ...

Can images be placed on the border?

Hi there! I am trying to add an image on a border with a 100px solid width using CSS. I have experimented with positions and z-index, but I can't seem to get the desired effect. Any ideas on how to achieve this? Here is an example image: https://i.sst ...

A guide on converting UTC time and date to local time and vice versa with MongoDB and Mongoose/Express

When comparing dates, I need to take into account my local Timezone (Asia/Kolkata GMT+5.30). var today = moment().toDate(); var tomorrow = moment().add(1,'days').toDate(); Moment : I am utilizing MomentJS for this task (but unsure if it's ...

transferring information from child to parent with the help of Vue.js and Laravel

As a newcomer to vue.js, I have a child component called 'test' and a parent component called 'showdata'. My issue arises when I try to emit data from the child to the parent - while the emission is successful, displaying the data in th ...

Is there a way to adjust the quantity of items in the shopping cart without the need to reload the webpage?

Currently, I am working on a test project using React & Redux. The project involves implementing a basket feature where I receive an array of products structured like this: products: [ { name: "Product one", count: 1, ...

Check in JavaScript if two dates are too close together and trigger an alert

Struggling to create a function that calculates the difference between two dates in JavaScript? Looking for help in displaying an error message if the dates are less than 20 days apart? The code snippet below is what I have so far, but as a beginner in Jav ...

Uncertain on the functionality of OnSubmit

I am currently working with the following code: <form onSubmit= {handleSubmit}> <div className="form-group"> <label htmlFor="focusedInput">Level</label> <input className="form-control" id="focusedInput" typ ...

Should arrays of objects be kept in databases or JavaScript files?

Here is a small excerpt from an array of objects utilized on my website. It's just a snippet and not the full JS file. I have several JS files like this, some reaching up to 500 lines of code. Currently delving into databases ...

Unable to retrieve array information within a reactjs environment

I encountered an issue while trying to retrieve data from the getObjectives function as it returned undefined. I am unsure whether the problem lies within the function itself or in the code implementation. Here is the GetObjectives() function: export asyn ...

Retrieve the text located between two specified texts using jQuery

In search of a way to extract the content between two tags located within the main div, I encountered the following illustration... <div class="main_result"> some text.... <div>other divs</div> <p>some other html</p> <div ...

Problem with jQuery Mobile Panel: Will not open when clicked a second time

I'm facing an issue with the left panel in my App. It works fine when I first build it, but once I try to go back to the main screen, the panel stops functioning. To replicate this bug, follow these steps: 1) Click on the 'Bars' Icon at the ...