插入一个JSONvariables

我的代码如下:

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中插入JSONvariables。 提前致谢。

Mongo collection insert方法接受文档或文档数组,并传递一个string给它。

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