MongoDB
 sql >> Datenbank >  >> NoSQL >> MongoDB

Dekrementieren eines Felds vom Typ Nummer in mongodb und nodejs

Verwenden Sie $inc Operator.

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

Aktualisiert:

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 
    }
});