【问题标题】:Unable to print BSON object from javascript无法从 javascript 打印 BSON 对象
【发布时间】:2019-02-28 19:14:22
【问题描述】:

我的 mongoDB 集合如下所示:

{
    "_id" : ObjectId("5070310e0f3350482b00011d"),
    "emails" : [
            {
                    "_id" : ObjectId("5070310e0f3350482b000120"),
                    "_type" : "Email",
                    "name" : "work",
                    "email" : "peter.loescher@siemens.com",
                    "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 时,它给出了正确的结果。即peter.loescher@siemens.com

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

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

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

【问题讨论】:

    标签: javascript mongodb find bson


    【解决方案1】:

    您需要printjson 才能输出格式正确的 JSON:

    printjson(c.emails[0]);
    

    这里是the documentation

    【讨论】:

    • 如果我想要紧凑的表格,不希望它打印多行怎么办?
    • @LewisChan 请提出另一个问题并描述您的要求
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 2022-08-05
    • 1970-01-01
    相关资源
    最近更新 更多