无法从javascript打印BSON对象

问题描述 投票:13回答:1

我的mongoDB集合看起来像这样:

{
    "_id" : ObjectId("5070310e0f3350482b00011d"),
    "emails" : [
            {
                    "_id" : ObjectId("5070310e0f3350482b000120"),
                    "_type" : "Email",
                    "name" : "work",
                    "email" : "[email protected]",
                    "current" : true
            }
    ]
}

这是我用来打印内容的.js代码:

c = db.contacts.findOne( { "emails.email" : { $ne : null } }, { "emails" : 1 } )

print(c._id.toString() + " " + c.emails[0]);

当我尝试运行这个javascript文件时,它只是显示id而不是电子邮件数组。

output:
5070310e0f3350482b00011d [object bson_object]

但是,当我尝试c.emails[0].email是给出正确的结果。即[email protected]

我只需要显示整个电子邮件嵌入对象。

i.e.
"emails" : [
        {
                "_id" : ObjectId("5070310e0f3350482b000120"),
                "_type" : "Email",
                "name" : "work",
                "email" : "[email protected]",
                "current" : true
        }
]

我哪里错了?任何帮助,将不胜感激。

javascript mongodb find bson
1个回答
31
投票

你需要printjson来输出一个格式很好的JSON:

printjson(c.emails[0]);

这是the documentation

© www.soinside.com 2019 - 2024. All rights reserved.