How to update a field in MongoDB

0

Considering the following structure:

{
"_id" : ObjectId("5ad69abb3630404194c80571"),
"cnpj" : 2352345234523452,
"razao_social" : "Lalala Ltda.",
"fantasia" : " "aiufhdiua Ltda.",
"dt_criacao" : ISODate("2018-04-18T01:09:15.076Z"),
"usuarios" : [ ],
"sensores" : [
    {
        "id" : 1,
        "leituras" : [ ]

    },
    {
        "id" : 2,
        "leituras" : [ ]
    }
]

}

How do I update this document with the document below only in the sensores.id:2 field?

{ "latitude": 45.435435, "longitude": 120.09345493 }

Thank you in advance!

    
asked by anonymous 19.04.2018 / 05:22

1 answer

0

Try running the following command in the mongo console: (replace <collection> with correct name)

db.<collection>.update({"sensores.id" : 2 }, { $set: { "sensores.$.leituras.latitude" : "45.435435", "sensores.$.leituras.longitude": "120.09345493"}})
    
19.04.2018 / 09:28