Home > other >  I got an error using mul operator in mongodb shell
I got an error using mul operator in mongodb shell

Time:05-31

I'm trying to multiply a value for 1.2 but there is showing me an error, could you help me finding the issue and how to solve it:

The next is the collection:

    db.test_sensores.find({'sensor_id':1,'location_id':2}).sort({'medidas.0.valor':1})
{ "_id" : ObjectId("6292434a995b9a2a8133ca64"), "timestamp" : "2020-07-01T00:15:00Z", "sensor_id" : 1, "location_id" : 2, "medidas" : [ { "tipo_medida" : "Temperatura", "valor" : 21.12, "unidad" : "ºC" }, { "tipo_medida" : "Humedad_relativa", "valor" : 37.7, "unidad" : "%" } ] }
{ "_id" : ObjectId("6292434a995b9a2a8133ca62"), "timestamp" : "2020-07-01T00:00:00Z", "sensor_id" : 1, "location_id" : 2, "medidas" : [ { "tipo_medida" : "Temperatura", "valor" : 22.08, "unidad" : "ºC" }, { "tipo_medida" : "Humedad_relativa", "valor" : 34.92, "unidad" : "%" } ] }
{ "_id" : ObjectId("6292434a995b9a2a8133ca66"), "timestamp" : "2020-07-10T00:30:00Z", "sensor_id" : 1, "location_id" : 2, "medidas" : [ { "tipo_medida" : "Temperatura", "valor" : 24.26, "unidad" : "ºC" }, { "tipo_medida" : "Humedad_relativa", "valor" : 52.52, "unidad" : "%" } ] }
{ "_id" : ObjectId("6292434a995b9a2a8133ca68"), "timestamp" : "2020-07-01T00:45:00Z", "sensor_id" : 1, "location_id" : 2, "medidas" : [ { "tipo_medida" : "Temperatura", "valor" : 24.51, "unidad" : "ºC" }, { "tipo_medida" : "Humedad_relativa", "valor" : 34.1, "unidad" : "%" } ] }

And this is what I'm trying to do:

    db.test_sensores.update({'ObjectId':'6292434a995b9a2a8133ca64'},{$mul:{medidas.0.valor:1.2}})
uncaught exception: SyntaxError: missing : after property id :
@(shell):1:78

CodePudding user response:

I found I was missing mark quotations and my objectid is inside _id, so replacing that value in this way I solved the issue, but could be another way or maybe the solution I provided is not a good practice?:

db.test_sensores.update({'_id':ObjectId("6292434a995b9a2a8133ca64")},{$mul:{'medidas.0.valor':1.2}})
  • Related