【问题标题】:Querying an embedded list in OrientDB在 OrientDB 中查询嵌入列表
【发布时间】:2015-02-05 12:19:46
【问题描述】:

我的 OrientDB 数据库(版本 1.0.1)中有一个文档,其结构大致如下:

{
    "timestamp": "...",
    "duration": 665,
    "testcases": [
        {
            "testName": "test01",
            "type": "ignore",
            "filename": "tests/test1.js"
        },
        {
            "iterations": 1,
            "runningTime": 45,
            "testName": "test02",
            "type": "pass",
            "filename": "tests/test1.js"
        },
        ...
        {
            "testName": "test05",
            "type": "ignore",
            "filename": "tests/test1.js"
        }
    ]
}

如何查询整个列表,例如。如果我想查找所有包含类型为“ignore”的测试用例的文档?

我尝试了以下查询

select from testresult where testcases['type'] = 'ignore'

但这会导致NumberFormatException

select from testresult where testcases[0]['type'] = 'ignore'

有效,但显然只查看每个文档的第一个列表元素。

select from testresult where testcases contains(type = 'ignore')

不提供任何结果,但查询被接受为有效。

更新: 如果测试用例存储为单独的文档而不是嵌入列表,则以下查询按预期工作。

select from testresult where testcases contains (type = 'ignore')

【问题讨论】:

  • 我遇到了和你一样的问题。您可以在问题中发布您的更新并接受它。这就是我想要的。
  • @DavidLaberge :感谢您的建议。这个问题太老了,坦率地说,我不完全记得它的上下文,所以我会对那个操作感到有点不安。我也有一种感觉,我最初的方法应该可行,至少根据 OrientDB 的文档,所以鼓励人们像上面那样重组他们的数据库是错误的。

标签: java orientdb


【解决方案1】:

我知道这是一个老问题,但我遇到了同样的问题,只是在这里找到了答案: https://www.mail-archive.com/orient-database@googlegroups.com/msg00662.html

以下应该有效。它适用于我非常相似的用例。

select from testresult where 'ignore' in testcases.type

【讨论】:

  • 通常的 sql 语句 where testcases.type in 'ignore' 不适用于 orientdb。 Orientdb 使用正确的英语:)
【解决方案2】:

我有类似的问题,最终得到:

select * from testresult 
  let $tmp = (select from 
    (select expand(testcases) from testresult ) 
  where 
    value.type = 'ignore') 
where 
  testcases in $tmp.value

这将为您提供包含至少一个类型为忽略的测试用例的所有测试结果文档。此查询适用于嵌入式列表。注意在 OrientDB >= 1.4.0 中可以使用扩展功能。

内部查询:

select from (select expand(testcases) from testresult) where value.type='ignore'

仅选择 type = 'ignore' 的不同测试用例。结果是测试用例。为了得到整个文档,我们将这些测试用例与每个文档中包含的测试用例($tmp.value 中的测试用例)进行匹配。

不知道有没有更简单的查询嵌入列表的方法...

【讨论】:

    【解决方案3】:

    试试

    select from testresult where testcases traverse ( type = 'ignore' )
    

    检查遍历运算符 (https://groups.google.com/forum/?fromgroups#!topic/orient-database/zoBGmIg85o4) 以了解如何使用 fetchplan 或将 any() 而不是测试用例放在“where”之后。

    例如,我们有一个名为 Country 的类,它有一个带有一些 isoCode 的嵌入式列表属性。如果我们尝试以下查询:

    select name,description,isoCodes,status from Country where isoCodes traverse ( value = 'GB' OR value = 'IT' )
    

    Orientdb Rest接口提供:

    {
      "result": [{
          "@type": "d", "@version": 0, 
    "name": "Italy", 
      "isoCodes": [
        {
          "@type": "d", "@version": 0, 
        "code": "iso3166-A2", 
        "value": "IT"
          }, 
        {
          "@type": "d", "@version": 0, 
        "code": "iso3166-A3", 
        "value": "ITA"
          }], 
      "status": [
        {
          "@type": "d", "@version": 0, 
        "status": "1", 
        "startingDate": "2012-04-24"
          }]
        }, {
          "@type": "d", "@version": 0, 
    "name": "United Kingdom", 
      "isoCodes": [
        {
          "@type": "d", "@version": 0, 
        "code": "iso3166-A2", 
        "value": "GB"
          }, 
        {
          "@type": "d", "@version": 0, 
        "code": "iso3166-A3", 
        "value": "GBR"
          }], 
      "status": [
        {
          "@type": "d", "@version": 0, 
        "status": "1", 
        "startingDate": "2012-04-24"
          }]
        }
      ]
    }
    

    希望对您有所帮助!!

    问候。

    【讨论】:

    • 感谢您的回复。不幸的是,它并没有改善我的情况。只要嵌入列表包含指向其他文档的链接,建议的查询就可以完美地工作,但是当列表中的对象只是值时不提供任何结果,即。当我更新我的问题时,我遇到了同样的情况。您确定您的 IsoCodes 属性不是数据库中的单独文档吗?
    猜你喜欢
    • 2014-05-18
    • 2016-07-26
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-21
    相关资源
    最近更新 更多