mongodb笔记
MongoDB 字段值加n
方法一:使用UpdateMany的$inc
db.getCollection("item").updateMany(
{id : 1002},
{$inc : {amount : NumberInt(1)}
});
方法二:使用forEach,先查找出当前值,再加
//将所有用户红包券道具+1
db.getCollection("item").find({id:1002}).forEach(
function(d){
db.getCollection("item").updateOne(
{user_id : d.user_id, id : d.id},
{$set : {amount : NumberInt(d.amount + 1)}
});
}
);
MongoDB 字段值乘n
//将所有用户红包券道具翻10倍
db.getCollection("item").find({id:1002}).forEach(
function(d){
db.getCollection("item").updateOne(
{user_id : d.user_id, id : d.id},
{$set : {amount : NumberInt(d.amount * 10)}
});
}
);