我写了XQuery来搜索JSON文档。代码如下:
xquery version "3.1"
let $characters := fn:json-doc("pott.json")?characters?*
return
document {
<characters>
{
for $s in fn:distinct-values($characters?name)
return
if ( $characters[?name eq $s]?house eq "Hufflepuff")
then
(
<characters>
<name>"{$s}"</name>
<datas>
<name>"{$characters[?name eq $s]?name}"</name>
<id>"{$characters[?name eq $s]?_id}"</id>
<deatheater>"{$characters[?name eq $s]?deathEater}"</deatheater>
<school>"{$characters[?name eq $s]?school}"</school>
</datas>
</characters>
)
else
()
}
</characters>
}
但是我也想添加一个布尔类型的搜索,例如:如果deathEater
的字符值= false
,则仅显示属于房屋Hufflepuff
和deathEater
= false
的字符]。这是我的JSON文件:
{
"characters":[
{
"house":"Slytherin",
"orderOfThePhoenix":false,
"name":"Phineas Nigellus Black",
"bloodStatus":"pure-blood",
"deathEater":false,
"dumbledoresArmy":false,
"school":"Hogwarts School of Witchcraft and Wizardry",
"role":"(Formerly) Headmaster of Hogwarts",
"__v":0,
"ministryOfMagic":false,
"_id":"5a0fa6bbae5bc100213c2334",
"species":"human"
},
{
"house":"Slytherin",
"orderOfThePhoenix":false,
"name":"Regulus Black",
"bloodStatus":"pure-blood",
"deathEater":true,
"dumbledoresArmy":false,
"school":"Hogwarts School of Witchcraft and Wizardry",
"__v":0,
"ministryOfMagic":false,
"_id":"5a0fa772ae5bc100213c2337",
"species":"human"
},
{
"house":"Gryffindor",
"orderOfThePhoenix":true,
"name":"Sirius Black",
"bloodStatus":"pure-blood",
"alias":"Padfoot",
"deathEater":false,
"dumbledoresArmy":false,
"school":"Hogwarts School of Witchcraft and Wizardry",
"animagus":"black dog",
"__v":0,
"ministryOfMagic":false,
"_id":"5a0fa7dcae5bc100213c2338",
"species":"human"
},
{
"house":"Hufflepuff",
"orderOfThePhoenix":true,
"name":"Amelia Bones",
"bloodStatus":"unknown",
"deathEater":false,
"dumbledoresArmy":false,
"school":"Hogwarts School of Witchcraft and Wizardry",
"role":"Head, Department of Magical Law Enforcement",
"__v":0,
"ministryOfMagic":true,
"_id":"5a0fa842ae5bc100213c2339",
"species":"human"
},
{
"house":"Hufflepuff",
"orderOfThePhoenix":false,
"name":"Susan Bones",
"bloodStatus":"half-blood",
"deathEater":false,
"dumbledoresArmy":true,
"school":"Hogwarts School of Witchcraft and Wizardry",
"role":"student",
"__v":0,
"ministryOfMagic":false,
"_id":"5a0fa86dae5bc100213c233a",
"species":"human"
}
}
]
}
您能帮我怎么做,我已经尝试了几种功能,但没有一个对我有用。
在XQuery中,使用函数true()
和false()
编写布尔值。您可以如下扩展条件:
使用