【问题标题】:For let, not incrementing if there is a same item in array对于 let,如果数组中有相同的项,则不递增
【发布时间】:2019-08-09 11:55:29
【问题描述】:

我正在尝试使用 find $in 数组增加我的 mongodb 中的一个项目,但是当一个数组中有一个相同的项目时,例如 ['apple','apple'] 它应该增加两次,但就我而言,它只增加一次,看在我的代码中:

 var newValue = 1;
 var newSerialcode = req.body.serialCode; 
 var newBloodgroup = req.body.blood_group; 
 var newGetbloodcomponent = req.body.blood_component; 

 Bloodinventory.find({ blood_component : { $in : newGetbloodcomponent} ,blood_group: { $in :newBloodgroup},chapter: { $in: [id] }}, function(err, bloodinventoryDocs) {

            for(let bloodinventory of bloodinventoryDocs) {
                bloodinventory.num_stock = bloodinventory.num_stock + newValue ;                        
                bloodinventory.save(function(err) {
                    if (err) {
                        console.log(err); 
                    } else {
                        console.log('success'); 
                    }
                });
            }  
        });

【问题讨论】:

  • 哪个数组应该有重复值?是bloodinventoryDocs 吗? find() 查询的返回值是什么?
  • 假设 newGetbloodcomponent 具有重复值,例如 ['whole blood','whole blood']
  • 我想你可能误解了$in 的用途。让我解释一下您的查询将如何以人类语言工作,您将找到所有血液库存,其中 blood_component 位于 newGetbloodcomponent 中,blood_group 位于 newBloodgroup 中,并且 chapter 位于 id 中。因此,即使您的数组中有重复值,您的查询也会返回相同的结果。
  • 您应该展示一些示例文档、您用于查询的参数以及您期望的结果。如前所述,在$in 中简单地提供“两次”值不会导致它“两次”获取匹配的文档。事实上,MongoDB 在处理之前实际上会在查询中“去除重复项”。应该如此。

标签: node.js angularjs mongodb mean-stack


【解决方案1】:

我知道这已经有一段时间了,但这可能仍然对您(或其他人)有所帮助,所以...

也许你可以试试这个:

var newValue = 1;
var newSerialcode = req.body.serialCode; 
var newBloodgroup = req.body.blood_group; 
var newGetbloodcomponent = req.body.blood_component; 

Bloodinventory.find({ blood_component : { $in : newGetbloodcomponent} ,blood_group: { $in :newBloodgroup},chapter: { $in: [id] }}, function(err, bloodinventoryDocs) {

    for(let bloodinventory of bloodinventoryDocs) {
        // === change starts here === //
            // filter into a new array all the places where this particular blood component occurs
            let all_occurences = newGetbloodcomponent.filter(function(b){
                return b === bloodinventory.blood_component;
            });

            // to avoid incurring unnecessary processing and/or database costs, only continue if an occurence was actually found
            if(all_occurences.length > 0){
                // increment it by that amount (which will be the number of items filtered into the array)
                bloodinventory.num_stock = bloodinventory.num_stock + all_occurences.length;
                // ^^ you could also write this as 'bloodinventory.num_stock += all_occurences.length;'
            
                bloodinventory.save(function(err) {
                    if (err) {
                        console.log(err); 
                    } else {
                        console.log('success'); 
                    }
                });
            };
        // === change ends here === //
    }  
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 2014-10-27
    • 2021-10-10
    • 2021-09-23
    • 1970-01-01
    相关资源
    最近更新 更多