Pregunta

Say the mongo Array, named matrix represents a 3 x 3 Matrix:

{
    "0" : 1,
    "_id" : "1",
    "height" : 3,
    "matrix" : [
        1,
        2,
        6,
        4,
        5,
        6,
        7,
        8,
        9
    ],
    "width" : 3
}

I would like to simply replace 5, with 15. Incrementing a specific location is a trivial operation:

db.best_time.update({_id:"1"},{ $inc: {"n.0" : 1 }})

Is there an equally simple operation to replace 5, with 15?

¿Fue útil?

Solución

You use the same basic approach, but use the $set operator instead of $inc:

db.best_time.update({_id:"1"}, {$set: {'matrix.4': 15}})
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top