我有一个这样的示例 mongo 文档:
> db.chat.find().pretty()
{
"_id": ObjectId("555f1c0c7f4b820758b439b0"),
"user": "Guest1",
"friend": [{
"userfriend": "Guest2",
"noidung": [{
"method": "send",
"date": "2015-05-22T19:11:34+07:00",
"content": "allloooo"
}, {
"method": "receive",
"date": "2015-05-23T09:08:14+07:00",
"content": "yes man"
}]
}, {
"userfriend": "Guest3",
"noidung": [{
"method": "send",
"date": "2015-05-23T15:42:34+07:00",
"content": "foo 15:42"
}, {
"method": "receive",
"date": "2015-05-23T15:42:45+07:00",
"content": "bar 15:43"
}]
}]
}
在我的 server.js 中,我使用这段代码来打印所有数据:
var chathistory = db.collection('chat');
chathistory.find().toArray(function (err, docs) {
console.log(docs)
});
我在我的终端中得到这个日志:
[ { _id: 555f1c0c7f4b820758b439b0,
user: 'Guest1',
friend: [ [Object], [Object] ] } ]
'friend' 字段不会打印全部,它只有 [Object]
,所以我怎样才能得到完整的数据。
最佳答案
要打印所有数据,请使用 JSON.stringify()
方法
db.collection('chat').find().toArray(function(err, docs) {
console.log(JSON.stringify(docs));
});
关于javascript - 打印所有mongoDB数据到字符串nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30671430/