【问题标题】:Print count of inserted documents after insertMany operation in Node在 Node 中的 insertMany 操作后打印插入文档的计数
【发布时间】:2021-08-23 17:31:37
【问题描述】:

此脚本运行良好,但我只想在插入操作成功时在控制台中显示插入文档的数量。

app.post('/',uploads.single('csv'),(req,res)=>{      
csv()  
.fromFile(req.file.path)  
.then((jsonObj)=>{  
     csvModel.insertMany(jsonObj,(err,data)=>{  
            if(err){  
                console.log(err);  
            }else{  
                console.log('Documents Inserted Successfully');
                res.redirect('/');  
            }  
     });  
   });  
});  

【问题讨论】:

  • 它将返回insertedIds,插入的文档ID数组,控制台data.insertedIds.length
  • 错误 ``` TypeError: 无法读取 C:\Users\User\cogazzimport\app.js:63:46 处未定义的属性“长度” ```
  • 只是控制台data你得到了什么。
  • 它是一个完整的json多维数组
  • 我认为我应该使用 data.length 并且它的工作原理!

标签: node.js json mongodb bulkinsert


【解决方案1】:

其实正确的答案是得到数据的长度

console.log(data.length);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多