Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

node.js - For let, not incrementing if there is a same item in array

I'm trying to increment an item in my mongodb using find $in array, but when there is a same item in an array for example ['apple','apple'] it should increment twice but for my case, it only increment once, look at my code:

 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'); 
                    }
                });
            }  
        });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I know this has been a while, but this might still help you (or maybe someone else), so...

Perhaps you can try this:

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 === //
    }  
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...