2

我已将我的数据存储在 AreangoDB 中的给定格式中,我在 DSP 中的集合名称:

 "data": {
"1":   [ {"db_name": "DSP"}, {"rel": "2"} ], 
"2":   [ {"rel_name": "DataSource"}, {"tuple": "201"}, {"tuple": "202"}, {"tuple": "203"} ],
"201": [ {"src_id": "Pos201510070"}, {"src_name": "Postgres"}, {"password": "root"}, {"host": "localhost"}, {"created_date": "20151007"}, {"user_name": "postgres"}, {"src_type": "Structured"}, {"db_name": "postgres"}, {"port": "None"} ],
"202": [ {"src_id": "pos201510060"}, {"src_name": "Postgres"},{"password": "root"}, {"host": "localhost"}, {"created_date": "20151006"}, {"user_name": "postgres"}, {"src_type": "Structured"}, {"db_name": "DSP"}, {"port": "5432"} ],
"203": [ {"src_id": "pos201510060"}, {"src_name": "Postgres"}, {"password": "root"}, {"host": "localhost"}, {"created_date": "20151006"}, {"user_name": "postgres"},{"src_type": "Structured"},{"db_name": "maindb"},{"port": "5432"} ]
}

我正在使用以下格式的上述数据执行查询:

  FOR p IN NestedDSP
  LET attributes = ATTRIBUTES(p) 
  FOR attribute IN attributes 
      LET key = ATTRIBUTES(attribute)[0] 
      LET value = attribute[key] 
      RETURN { subject: attribute, predicate: key, object: value }

当我向 ArangoDB 提交查询时,它返回的响应为:

    Warnings:

[1542], 'invalid argument type in call to function 'ATTRIBUTES()''
[1542], 'invalid argument type in call to function 'ATTRIBUTES()''
[1542], 'invalid argument type in call to function 'ATTRIBUTES()''
[1542], 'invalid argument type in call to function 'ATTRIBUTES()''

Result:

[
  {
    "subject": "data",
    "predicate": null,
    "object": null
  },
  {
    "subject": "_id",
    "predicate": null,
    "object": null
  },
  {
    "subject": "_rev",
    "predicate": null,
    "object": null
  },
  {
    "subject": "_key",
    "predicate": null,
    "object": null
  }
]

请告诉我这个查询有什么问题,为什么答案像上面那样。我在 ArangoDB-2.7.3-win64 工作。

谢谢

4

1 回答 1

3

让我演示如何构建这样一个深入挖掘嵌套数据结构的复杂查询。我开始获取查询的外部部分,以获得内部结果:

FOR p IN NestedDSP
  LET attributes = ATTRIBUTES(p) 
  FOR attribute IN attributes 
      RETURN attribute

这给了我:

[ 
  "data", 
  "_rev", 
  "_key", 
  "_id" 
]

因此,让我们更深入地了解下一层。我猜您只对data键下方的值感兴趣,对吗?所以我们选择p.data

FOR p IN NestedDSP
  LET attributes = ATTRIBUTES(p.data) 
  FOR attribute IN attributes 
      RETURN attribute

然后给我你的下一个内部数组的键:

[ 
  "203", 
  "202", 
  "201", 
  "2", 
  "1" 
]

我们现在探索我们发现附加到这些节点的内容:

FOR p IN NestedDSP
  LET attributes = ATTRIBUTES(p.data) 
  FOR oneAttribute IN attributes 
    LET keys = p.data[oneAttribute]
      RETURN keys

它又是一个数组,我们需要使用FOR循环键迭代它:

[
  [ 
    { 
      "src_id" : "pos201510060" 
    }, 
    { 
      "src_name" : "Postgres" 
    }, ...

我们添加了这个额外FOR的循环:

FOR p IN NestedDSP
  LET attributes = ATTRIBUTES(p.data) 
  FOR oneAttribute IN attributes 
    LET keys = p.data[oneAttribute]
    FOR key IN keys
      RETURN key

我们得到最里面的对象:

[ 
  { 
    "src_id" : "pos201510060" 
  }, 
  { 
    "src_name" : "Postgres" 
  }, 
  { 
    "password" : "root" 
  }, 
...

您想使用该ATTRIBUTES函数,但对象只有一个成员,因此我们可以访问[0]

FOR p IN NestedDSP
  LET attributes = ATTRIBUTES(p.data) 
  FOR oneAttribute IN attributes 
    LET keys = p.data[oneAttribute]
    FOR key IN keys
      LET keyAttributes=ATTRIBUTES(key)
        RETURN keyAttributes

这给了我们对象键,每个最里面的对象一个:

[ 
  [ 
    "src_id" 
  ], 
  [ 
    "src_name" 
  ], 

我们检查我们现在是否只得到最内层结构的对象键;我们选择比上面更聪明的变量名:

  FOR p IN NestedDSP
    LET attributes = ATTRIBUTES(p.data) 
    FOR oneAttribute IN attributes 
      LET pairs = p.data[oneAttribute]
      FOR onePair IN pairs
        LET pairKey=ATTRIBUTES(onePair)[0]
          RETURN pairKey

是的:

[ 
  "src_id", 
  "src_name", 
  "password", 
  "host", 
    ...

所以现在是时候根据需要构建结果对象了:

  FOR p IN NestedDSP
    LET attributes = ATTRIBUTES(p.data) 
    FOR oneAttribute IN attributes 
      LET pairs = p.data[oneAttribute]
      FOR onePair IN pairs
        LET pairKey=ATTRIBUTES(onePair)[0]
          RETURN { subject: oneAttribute, predicate: pairKey, object: onePair[pairKey] }

subject是标识最外层项目的数字,是predicate对象键,object是其中的值:

[ 
  { 
    "subject" : "203", 
    "predicate" : "src_id", 
    "object" : "pos201510060" 
  }, 
  { 
    "subject" : "203", 
    "predicate" : "src_name", 
    "object" : "Postgres" 
  }

希望哪一个是你想要的?

于 2016-06-20T16:17:12.530 回答