Search code examples
mongodbexpressmean-stack

Not iterable when using find ObjectId


I'm trying to find a certain document in my mongodb then update the int value of it using find query, I'm using $in because I used an array to find each element inside it, but when I used ObjectId it gives me error:

bloodinventoryDocs is not iterable

Here is what I did

  var mongoose = require('mongoose');
  var id = mongoose.Types.ObjectId('5c014c999cc48c3b0057988b');
  var newValue = 1;
  var newBloodgroup = "A_positive"; 
  var newGetbloodcomponent = "Whole Blood"; 


        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'); 
                    }
                });
            }  
        });

Solution

  • Just use chapter: { $in: [id] }