简体   繁体   中英

MongoDB: Decrement all documents with field value higher then defined

I want to decrement all documents with order greater then a given value and a given parent value:

var order = 5, parent = "books";
Articles.update({ parent: parent, order > order }, $inc { order: -1 }, {multi: true});

In this example all documents with parent = books and with a higher order value of 5 are updated to an decremented order value. I don't know if this is possible with one line or do I have to start the update() multiple times in a loop?

You're very close already. Here's a list of comparison operators for MongoDB

var order = 5,
    parent = 'books'

Articles.update(
    { parent : parent, order : { $gt : order } },
    { $inc : { order : -1 } },
    { multi : true }
)

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