gpt4 book ai didi

javascript - 打印所有mongoDB数据到字符串nodejs

转载 作者:行者123 更新时间:2023-11-30 12:22:03 25 4
gpt4 key购买 nike

我有一个这样的示例 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com