I have a sample mongo document like this:
> 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"
}]
}]
}
And in my server.js, i use this code to print all data:
var chathistory = db.collection('chat');
chathistory.find().toArray(function (err, docs) {
console.log(docs)
});
And i get this log in my terminal:
[ { _id: 555f1c0c7f4b820758b439b0,
user: 'Guest1',
friend: [ [Object], [Object] ] } ]
'friend' field doesn't print all, its only [Object]
, so how can i get full data.
I have a sample mongo document like this:
> 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"
}]
}]
}
And in my server.js, i use this code to print all data:
var chathistory = db.collection('chat');
chathistory.find().toArray(function (err, docs) {
console.log(docs)
});
And i get this log in my terminal:
[ { _id: 555f1c0c7f4b820758b439b0,
user: 'Guest1',
friend: [ [Object], [Object] ] } ]
'friend' field doesn't print all, its only [Object]
, so how can i get full data.
- You have to use json.stringify(docs) . – shreyansh Commented Jun 5, 2015 at 16:31
-
@shreya
console.log(require('util').inspect(docs, { showHidden: true, depth: null }));
will also work – thefourtheye Commented Jun 5, 2015 at 16:33 - 1 Tks @shreya:) you save my time – Thanhtu150 Commented Jun 5, 2015 at 16:34
- but json.stringify() is more simple to use. – shreyansh Commented Jun 5, 2015 at 16:34
1 Answer
Reset to default 5To print all the data, use the JSON.stringify()
method
db.collection('chat').find().toArray(function(err, docs) {
console.log(JSON.stringify(docs));
});