在我的超级账本编辑器应用程序中,我想编写一个命名查询,该查询返回具有两个指定爱好的所有人。
“人”的模型如下:
participant Person identified by id {
o String id
o String firstName
o String lastName
o String email
--> Hobby[] hobbies optional
}
“爱好”的模型如下:
asset Hobby identified by id {
o String id
o String name
}
命名查询具有以下结构:
query selectPersonsByHobbies {
description: "Select all persons with the two specified hobbies."
statement:
SELECT org.comp.myapp.Person
WHERE //not sure what to put here//
}
我不确定为了实现我想要的东西而在“ WHERE”运算符后面加上什么。
这正确吗?:
query selectPersonsByHobbies {
description: "Select all persons with the two specified hobbies."
statement:
SELECT org.comp.myapp.Person
WHERE (hobbies CONTAINS ((name == _$hobby1) AND (name == _$hobby2)))
}
还是以下正确??
query selectPersonsByHobbies {
description: "Select all persons with the two specified hobbies."
statement:
SELECT org.comp.myapp.Person
WHERE (hobbies CONTAINS (name == _$hobby1) AND CONTAINS (name == _$hobby2))
}
更新:
按照保罗·奥马洪尼(Paul O'Mahony)的回答,这是我对情况的理解:
为“人”提供以下模型:
participant Person identified by id {
o String id
o String firstName
o String lastName
o String email
o Hobby[] hobbies optional
}
和以下用于Hobby的模型:
asset Hobby identified by id {
o String id
o String name
}
以下查询将成功返回所有具有两个指定爱好的人:
query selectPersonsByHobbies {
description: "Select all persons with the two specified hobbies."
statement:
SELECT org.comp.myapp.Person
WHERE ( hobbies CONTAINS [ _$hobby1, _$hobby2] )
}
....与查询一起发送的参数值(分别插入_ $ hobby1和_ $ hobby2)将是相应爱好的ID,对吗?
答案 0 :(得分:1)
目前,您不能以Composer查询语言,以您建议的方式(目前可以使用OR)在“ Concepts for CONTAINS”中使用聚合“ AND”(或可以,但不能)-比较名称字段每个类条目(使用AND不能同时使用两者)。从0.19.12开始,您可以使用只读Trxn Processor函数中的getnativeAPI()
来对CouchDB查询语言进行等效的CONTAINS调用。
如果字段是String数组,例如String[] hobbies
,则
query selectPersonsByHobbies {
description: "Select all persons with the two specified hobbies."
statement:
SELECT org.comp.myapp.Person
WHERE ( hobbies CONTAINS [ _$hobby1, _$hobby2] )
}
但是您在Person
上的字段必须为Hobby[] hobbies optional
(即不是关系数组,即当前的关系ID-)。