使用find ObjectId不可迭代

问题描述 投票:1回答:1

我正在尝试在mongodb中找到某个文档,然后使用find查询更新它的int值,我使用了$ in,因为我使用了数组来查找其中的每个元素,但是当我使用ObjectId时它给我错误:

bloodinventoryDocs is not iterable

这是我所做的

  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'); 
                    }
                });
            }  
        });
mongodb express mean-stack
1个回答
1
投票

只需使用chapter: { $in: [id] }

© www.soinside.com 2019 - 2024. All rights reserved.