首页 > 解决方案 > 在 hyperledger-composer 的“命名查询”中访问数组属性

问题描述

在我的超级账本作曲家应用程序中,我想编写一个命名查询,返回所有具有特定爱好的人。

“人”的模型如下:

participant Person identified by id {
  o String id
  o String firstName
  o String lastName
  o String email
  --> Hobby[] hobbies optional
}

“爱好”的模型如下:

asset Hobby identified by name {
  o String name
}

命名查询具有以下结构:

query selectPersonsByHobby {
  description: "Select all persons with a certain hobby."
  statement:
      SELECT org.comp.myapp.Person
          WHERE //don't know what to put here//
}

我不知道在“WHERE”运算符之后放什么来实现我想要的。

我想要以下内容:

query selectPersonsByHobby {
      description: "Select all persons with a certain hobby."
      statement:
          SELECT org.comp.myapp.Person
              WHERE (hobbies.contains(_$hobby))
}

这甚至可能吗?

标签: hyperledger-composernamed-query

解决方案


简短的回答是这个查询应该有效:

query selectConsultantsBySkill {
  description: "Select all persons with a certain hobby."
  statement:
      SELECT org.comp.myapp.Person
          WHERE (hobbies CONTAINS _$targetHobby)
}

但请注意,因为您的爱好是一个关系数组,所以 targetHobby 参数必须类似于resource:org.acme.Hobby#cycling. 在生产场景中,您将从 UI 程序“调用”查询,以便您可以预先挂起关系语法。

我想这只是一个测试示例,但我想知道 Hobby 是否需要建立关系?如果没有会更容易。

您也可以使用概念(甚至是概念中的枚举类型!)。这是一个修改后的模型和带有概念的查询的示例:

participant Person identified by id {
  o String id
  o String firstName
  o String lastName
  o String email
  o Interest[] passTime
}

concept Interest {
  o String name
  o String description
}


query selectConsultantsBySkill {
  description: "Select all persons with a certain hobby."
  statement:
      SELECT org.comp.myapp.Person
          WHERE (passTime CONTAINS (name == _$targetHobby ))
}

推荐阅读