展平非常嵌套的mongoDB数据

时间:2017-06-04 20:43:43

标签: mongodb schema

我有一个非常深层嵌套的mongoDB架构,我必须弄平,因为我有一个无法用当前结构有效制作的复杂查询。我之前问过这个问题,但这个问题更为详细。这是架构的MWE:

db.test.insert({
    "_id" : ObjectId("58e574a768afb6085ec3a388"),
    "thing1": "a thing",
    "thing2": "another thing",
    "otherSchema": [
        {
        "z": "123456",
        "y": "987654"
        }
        ],
    "tests" : [
      {
        "_id" : ObjectId("58e542fb68afb6085ec3a1d2"),
        "details" : [
            {
            "a" : [
              {
                "unit" : "08",
                "size" : "5",
                "pos" : "Far",
                "_id" : ObjectId("58e542fb68afb6085ec3a1d6")
              }
            ],
            "b" : [
              {
                "pos" : "Drive Side Far",
                "size" : "5",
                "unit" : "08",
                "_id" : ObjectId("58e542fb68afb6085ec3a1d3")
              }
            ],
            "c" : [
              {
                "pos" : "Far",
                "size" : "3",
                "unit" : "08",
                "_id" : ObjectId("58e542fb68afb6085ec3a1d4")
              }
            ],
          "d" : [
              {
                "pos" : "Far",
                "size" : "5",
                "unit" : "08",
                "_id" : ObjectId("58e542fb68afb6085ec3a1d5")
              }
            ]
          }
        ]
      }
    ]
  }
)

我想要的输出是:

"_id" : ObjectId("58e574a768afb6085ec3a388"),
"thing1": "a thing",
"thing2": "another thing",
"otherSchema": [
    "z": "123456",
    "y": "987654"
    ],
"tests" : [
    {
        "_id" : ObjectId("58e542fb68afb6085ec3a1d2"),
        "aUnit" : "08",
        "aSize" : "5",
        "aPos" : "Far",
        "bPos" : "Far",
        "bSize" : "5",
        "bUnit" : "08",
        "cPos" : "Far",
        "cSize" : "3",
        "cUnit" : "08",
        "dPos" : "Far",
        "dSize" : "5",
        "dUnit" : "08"
            }
        ]

this previously asked/answered question中,它在该问题中满足MWE,但在添加更多信息时失败,如上所述。但是,该答案的推导将非常有用。我确实想要替换现有数据。

您能解释所采取的步骤,以便将其应用于更大的数据库吗?

0 个答案:

没有答案