sql >> Database teknologi >  >> NoSQL >> MongoDB

For let, ikke stigning, hvis der er et samme element i array

Jeg ved, at det har været et stykke tid, men det kan stadig hjælpe dig (eller måske en anden), så...

Måske kan du prøve dette:

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



  1. $geoNear matchende nærmeste array

  2. Meteor:Tracker.autorun / observeChanges &samlinger fungerer ikke som forventet

  3. HBase prøvetabel

  4. Forbedre MongoDB aggregeringsstruktur