有没有一种方法可以防止{} 1字段被打印并打印内部值?

时间:2019-05-03 13:27:57

标签: mongodb mongodb-compass

我正在使用mongodb指南针查询mongodb,但无法以表格形式打印该值,它始终打印{} 1个字段。我想将值作为表格打印在内部。当我使用mongoexport时也会发生同样的事情

Here is the original json structure

Here is the query structure

These are the results

2 个答案:

答案 0 :(得分:1)

这是因为find()命令中的项目无法规范对象结构。因此,生日返回的输出字段仍然是只有1个字段的对象。像{roleID: 9, formAnswers : { birthday : { value : "06/29/1981 17:04:29"}}}

要获得期望的输出,您应该使用聚合管道,这可以使其成为顶级字段。像db.coll.aggregate({$project : {roleID:1, birthday : "$formAnswers.birthday.value"}})

答案 1 :(得分:1)

您应该使用聚合管道,如下图所示: enter image description here

另一种选择是对以下查询使用Robo3T(例如Robomongo):

db.getCollection('test').aggregate([ { '$addFields': {'birthdayValue': '$formAnswer.birthday.value'}}, {'$project': {'_id': 0, 'roleID': 1, 'birthdayValue': 1}}])

您将得到其他图片所示的内容: enter image description here

相关问题