【发布时间】:2018-09-05 13:15:57
【问题描述】:
我有一个像这样的猫鼬模式:
{
"_id" : ObjectId("5a7acda13b808dbed05d6505"),
"symbol" : "@AKS",
"counter" : 4
},
{
"_id" : ObjectId("5a7acda13b808dbed05d6506"),
"symbol" : "@AKD",
"counter" : 5
}
现在,如果我想在一个查询上更新多个列,即“符号”值,那么 MongoDB updateMany Query 就可以了。这是查询:
MyCollectionName.updateMany({ 'symbol' : { $in : req.body.array}}, { $inc : { 'counter': 1 } }, function(err, data) {
if(err) {
console.log('error')
} else {
console.log('value incremented')
}
});
通过这个查询如果我给出数组值,即
var array = ["@AKS", "@AKD"];
然后它会增加Both的计数器。我的问题是如果我将在数组上提供相同的值,那么我想要两个增量而不是一个。像这样:
var array = ["@AKS", "@AKS"];
//I want the Increment of 2.
var array = [@AKS", "@AKS", "@AKS", "@AKS"]
//at this stage I want the counter of Increment is 4
但目前它只会做一个。这可能吗???? 非常感谢任何帮助。
【问题讨论】:
标签: arrays node.js mongodb mongoose mongodb-query