Reputation: 177
The project I am working on uses MongoDB, but I usually use RethinkDB. I am trying to write an operation that will take the current value of a document's property and subtract 1 from it. In reQL this would be r.db('db').table('table').get('documentId').update({propToUpdate: r.row('propToUpdate').sub(1)})
. How can I preform the same function in MongoDB?
Upvotes: 0
Views: 97
Reputation: 861
I think you can use $inc operator:
db.products.update(
{ sku: "abc123" },
{ $inc: { quantity: -1} }
);
As an example extract from here.
Where "db" is the database, (e.g. use myDatabase), "products" is the name of the collection you want to query, "sku" is the field you are querying and "quantity" is the field you want to increment/decrement.
Upvotes: 1