I currently have this data structure:
db.mydatabase:
{
"_id" : ObjectId("5ee50f412312e02d446aee2"),
"descripcion" : "filtros de aceite",
"array_repuesto" : [
{ --> I need this object!
"referencia" : "referencia3",
"marca" : "adidas2",
"precio" : 900012
},
{
"referencia" : "referencia2",
"marca" : "nike",
"precio" : 8000
}
],
"__v" : 0
},
{
"_id" : ObjectId("5ee50f40a2323d446aee2"),
"descripcion" : "filtros de agua",
"array_repuesto" : [
{
"referencia" : "referencia5",
"marca" : "adidas2",
"precio" : 900012
},
{
"referencia" : "referencia7",
"marca" : "nike",
"precio" : 8000
}
],
"__v" : 0
},
{
"_id" : ObjectId("5ee50f40a752be023232"),
"descripcion" : "filtros de arroz",
"array_repuesto" : [
],
"__v" : 0
}
I'm filtering the object where referencia = 'referencia3', but I don't know how to make it only return the updated
db.mydatabase.findOneAndUpdate(
{
_id: body._id,
"array_repuesto.referencia": "referencia3",
},
{
$set:
{
'array_repuesto.$.marca': 'SOS',
'array_repuesto.$.precio': '0',
},
},
(err, repuestoDb) => {
.
.
.
Output desired:
{
"referencia" : "referencia3",
"marca" : "adidas2",
"precio" : 900012
},
how can I do it? (by the way, is there a way to get the object back to me before updating?)