admin管理员组

文章数量:1395151

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.

Share Improve this question edited Jun 5, 2015 at 16:31 rene 42.5k78 gold badges121 silver badges165 bronze badges asked Jun 5, 2015 at 16:28 Thanhtu150Thanhtu150 912 silver badges16 bronze badges 4
  • 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
Add a ment  | 

1 Answer 1

Reset to default 5

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

db.collection('chat').find().toArray(function(err, docs) {
    console.log(JSON.stringify(docs));
});

本文标签: javascriptPrint all mongoDB data to string nodejsStack Overflow