MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Decremento di un campo di tipo number in mongodb e nodejs

Usa $inc operatore.

 db.yourcollection.update({ /* find options */ }, { $inc: {piecesLeft : -1 } });

Aggiornato:

db.yourcollection.update({
    _id: '572f16440a0dbb1e0cc02201', // Find updated item e.g. by _id
    piecesLeft: { $gt: 0 } // Update only if piecesLeft > 0
}, {
    $inc: {
        piecesLeft: -1 // Increment by -1 
    }
});