猫鼬DB多人更新同一字段

时间:2019-02-19 15:37:06

标签: mongodb mongoose

这是我的MongoDB模型

var Ingredients = new Schema({
    name: { type: String, required: true,unique: true},
    orignalPrice : {type: Number},
    discountprice :{type: Number},
    discountedCustomerPrice :{type:Number},
    image:{type:String}
});

我想要从前端通过百分比,然后它将所有文档“ discountprice”与原始价格分别更新为与

相同的百分比
 ingredientsModel.find({},async function (err,data){
        console.log(data);
        discountPercentage =  req.body.percentage / 100;
        for(let i = 0 ; i < data.length ; i++){
            data[i].discountprice  = data[i].orignalPrice * discountPercentage ;
                await data[i].save();
        }
        res.json({error:false,message:data});
});

以上工作正常,但我只想在单行查询或管道中做

0 个答案:

没有答案
相关问题