简体   繁体   中英

What is MongoDB's equivalent to RethinkDB's r.row?

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?

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, (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM