从ArangoDB中的嵌套文档中获取特定键

时间:2017-09-19 06:22:01

标签: arangodb

"Alpha":
    {"ABC":
     {"DEF":
         {"2017":
             {"GHI JKL":
                 {"MNO":
                     ["ABCDEFGHIJKLMNOP"]}},
             {"TGY BUG":
                  {"MNO":
                      ["1234567891012456"]}}}}}
"Alpha1":
    {"XYZ":
        {"TUW":
           {"2014":
               {"QRS PQR":
                    {"MNO":
                       ["ZYXWUTSRQPONML"]}}}}}
"Alpha2":
    {"KJM":
       {"LKI":
           {"2005":
              {"MNO":
                 ["POLKIKJUMNHY"]}}}

"Alpha3":
    {"WWW":
       {"CCC DDD":
           {"2011":
               {"MNO":
                    {"POLKIKJUMNHY":"value"}}}}}

我在ArangoDB中有四个文档Alpha,Alpha1,Alpha2和Alpha3。我想获取与密钥" MNO"相关的数据。来自单个查询中的每个文档。

1 个答案:

答案 0 :(得分:0)

(来自https://groups.google.com/forum/#!topic/arangodb/LshKkQMH7lg的交叉发布)

如果我理解正确,您希望在任意深度检索属性键为" MNO"的所有属性值。

没有以某种方式展平对象的功能,并且在AQL中无法进行递归。

如果它始终处于相同的嵌套级别(如果存在),则可以进行硬编码。

LET doc = {
  "_id": "remove-me",

  "Alpha": {
    "ABC": {
      "DEF": {
        "2017": {
          "GHI JKL": {
            "MNO": ["ABCDEFGHIJKLMNOP"],

            "PQR": true
          },
          "TGY BUG": {
            "MNO": ["1234567891012456"],
            "WXY": 987
          }
        }
      }
    }
  }
}


FOR a1 IN ATTRIBUTES(doc, true)
  FOR a2 IN ATTRIBUTES(doc[a1], true) || []
    FOR a3 IN ATTRIBUTES(doc[a1][a2], true) || []
      FOR a4 IN ATTRIBUTES(doc[a1][a2][a3], true) || []
        FOR a5 IN ATTRIBUTES(doc[a1][a2][a3][a4], true) || []
          FOR a6 IN ATTRIBUTES(doc[a1][a2][a3][a4][a5], true) || []
            FILTER a6 == "MNO"
              RETURN doc[a1][a2][a3][a4][a5]["MNO"]
              // --or-- if you want to return an object {"MNO": value} for each match
              //RETURN KEEP(doc[a1][a2][a3][a4][a5], "MNO")

我尝试了类似下面的内容,但无法让它选择" MNO"属性来自不同层次。也许你可以解决这个问题。

LET doc = {
  "_id": "remove-me",

  "Alpha": {
    "ABC": {
      "DEF": {
        "2017": {
          "GHI JKL": {
            "MNO": ["ABCDEFGHIJKLMNOP"],

            "PQR": true
          },
          "TGY BUG": {
            "MNO": ["1234567891012456"],
            "WXY": 987
          }
        }
      }
    },
    "DEF2": {
      "MNO": 67
    }
  },
  "Beta": {
    "MNO": "Beta MNO",
    "XXX": false,
    "YYY": null
  }
}


LET v0 = []
FOR a1 IN ATTRIBUTES(doc, true)
  LET v1 = APPEND(v0, a1 == "MNO" ? doc["MNO"] : [])
  FOR a2 IN ATTRIBUTES(doc[a1], true) || []
    LET v2 = APPEND(v1, a2 == "MNO" ? doc[a1]["MNO"] : [])
    FOR a3 IN ATTRIBUTES(doc[a1][a2], true) || []
      LET v3 = APPEND(v2, a3 == "MNO" ? doc[a1][a2]["MNO"] : [])
      FOR a4 IN ATTRIBUTES(doc[a1][a2][a3], true) || []
        LET v4 = APPEND(v3, a4 == "MNO" ? doc[a1][a2][a3]["MNO"] : [])
        FOR a5 IN ATTRIBUTES(doc[a1][a2][a3][a4], true) || []
          LET v5 = APPEND(v4, a5 == "MNO" ? doc[a1][a2][a3][a4]["MNO"] : [])
          FOR a6 IN ATTRIBUTES(doc[a1][a2][a3][a4][a5], true) || []
            LET v6 = APPEND(v5, a6 == "MNO" ? doc[a1][a2][a3][a4][a5]["MNO"] : [])
            RETURN v6