Node.js 插入JSON变量

Node.js 插入JSON变量,node.js,mongodb,mongoskin,Node.js,Mongodb,Mongoskin,我的代码如下: req.on('data', function(chunk) { console.log(JSON.stringify(chunk.toString('utf8'))); db.collection('collectionname').insert(chunk.toString('utf8'), function(err, result) { if (err) throw err; if (result) console.log

我的代码如下:

req.on('data', function(chunk) {
    console.log(JSON.stringify(chunk.toString('utf8')));
    db.collection('collectionname').insert(chunk.toString('utf8'), function(err, result) {
         if (err) throw err;
         if (result) console.log("Added!");
    });
 });
console.log
以JSON格式打印正确的消息,但此代码无法在MongoDB中插入区块


所以我的问题是如何在MongoDB中插入JSON变量。提前感谢。

insert
Mongo
collection
方法接受文档或文档数组,并向其传递字符串

db.collection('collectionname').insert({
  chunk: chunk.toString('utf8')
}, function(err, result){
  //...
})

为什么不能呢?有错误信息吗?另外,我建议您查看此答案,因为您似乎犯了相同的错误。数据以块的形式接收,只有在合并时才有意义。