插入一个 JSON 变量
Inserting a JSON variable
我的代码如下:
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){
//...
})
我的代码如下:
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){
//...
})