Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I push nested array values to a mongodb document?

If I have a document in mongodb with this structure:

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

How would I be able to push a new array containing lon and lat within the loc list?

The structure I am trying to use is this: db.collection('location').update({_id:'user1'},{'$push': {"lat": "-107.10400390625", "lon": "33.32343323432" }})

This obviously does not work because the lat and lon are nested within loc.

like image 974
Jimbobway Avatar asked Dec 04 '25 11:12

Jimbobway


1 Answers

db.collection('location').update({_id:'user1'},{'$push': { "loc": {"lat": "-107.10400390625", "lon": "33.32343323432" }}})
like image 121
danmactough Avatar answered Dec 07 '25 00:12

danmactough



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!