1

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.

4
  • You have to use json.stringify(docs) . Commented Jun 5, 2015 at 16:31
  • @shreya console.log(require('util').inspect(docs, { showHidden: true, depth: null })); will also work Commented Jun 5, 2015 at 16:33
  • 1
    Tks @shreya:) you save my time Commented Jun 5, 2015 at 16:34
  • but json.stringify() is more simple to use. Commented Jun 5, 2015 at 16:34

1 Answer 1

5

To print all the data, use the JSON.stringify() method

db.collection('chat').find().toArray(function(err, docs) {
    console.log(JSON.stringify(docs));
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.