我在我的视图功能中有这个:
emit([doc.address.country,doc.address.state, doc.address.city], doc);
当我查询搜索时,我需要填充数组的所有3个元素,例如:
?key=["US","NY","New York"]
这将产生我的记录,但举个例子,我只想返回美国的所有内容,例如:
?key=["US"]
或在美国和州......
?key=["US","NY"]
或者......或许我想也许我只想要来自纽约的所有记录......(我知道下面的内容不起作用)
?key=["","NY"]
如果你想将数组中的一个元素留空,我真的不知道如何搜索?
答案 0 :(得分:43)
首先:
key = [“US”]将无法在数组键[“US”,“NY”]上工作,因为您正在寻找一个完全[“US”]的键。相反,你必须使用
startkey=["US"]&endkey=["US",{}]
然后这些键在结果集中:
["DE","Bavaria","Munich"] <---- NO ! "DE" is out of Range of startkey
["US","FL","Miami"] <---- YES, starts with "US"
["US","NY","New York"] <---- YES, starts with "US"
["VE","XX","Vencuela City"] <---- NO ! "VE" is out of Range of endkey
同时工作:
startkey=["US","FL"]&endkey=["US","FL",{}]
结果:
["DE","Bavaria","Munich"] <---- NO ! "DE" is out of Range of startkey
["US","FL","Miami"] <---- YES, starts with "US","FL"
["US","NY","New York"] <---- NO, "US","NY" is out of Range of endkey
["VE","XX","Vencuela City"] <---- NO ! "VE" is out of Range of endkey
<强>第二强> 你不能在左侧有空白..所以你必须写一些更多的发射: (如果不需要查询,则不必发出第二个和第三个数组项)
查看“byStateCityCountry”:
emit([doc.address.state, doc.address.city,address.country], doc);
查看“byCityStateCountry”:
emit([address.city,doc.address.state, doc.address.country], doc);
只是在第一个位置放置一个标志来确定查询类型,因此您可以在一个视图中执行所有操作:
emit([1,address.country,doc.address.state, doc.address.city], doc);
emit([2,doc.address.state, doc.address.city,address.country], doc);
emit([3,address.city,doc.address.state, doc.address.country], doc);
用法:
?startkey=[1,"US"]&endkey=[1,"US",{}]
?startkey=[2,"FL"]&endkey=[2,"FL",{}]
?startkey=[3,"Miami"]&endkey=[3,"Miami",{}]