couchdb 搜索或过滤键数组 [英] couchdb search or filtering on key array
问题描述
我的视图函数中有这个:
I have this in my view function:
emit([doc.address.country,doc.address.state, doc.address.city], doc);
当我查询搜索时,需要把数组的3个元素都填满,例如:
When I query the search, I need to have all 3 elements of the array filled in, for example:
?key=["US","NY","New York"]
这将生成我的记录,但例如,我只想返回美国的所有内容,例如:
that will produce my records, but lets say for example, I just want to return everything in the US for example:
?key=["US"]
或者在美国和州...
or in the US and State...
?key=["US","NY"]
或者...可以说也许我只想要来自纽约的所有记录...(我知道以下不起作用)
OR... lets say perhaps I want just all records from NY... (i know the below doesn't work)
?key=["","NY"]
如果您想将数组的元素之一留空,我真的不知道如何搜索?
I don't really get how to search if you want to leave one of the elements of the array empty?
推荐答案
第一:
key=["US"] 对数组键 ["US","NY"] 不起作用,因为您正在寻找精确 ["US"] 的键.相反,你必须使用
key=["US"] will not work on an Array Key ["US","NY"], cause you're looking for a key that is EXACT ["US"]. Instead, you have to use
startkey=["US"]&endkey=["US",{}]
然后这些键在结果集中:
then those Keys are in the resultset:
["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
第二:你不能在左边有空格..所以你必须写更多的发射:(如果不需要查询,则不必发出第二个和第三个数组项)
Second: You cannot have blanks on left side.. so you have to write some more emits: ( you do not have to emit the second and third array-item, if you do not need to query it)
查看byStateCityCountry":
view "byStateCityCountry":
emit([doc.address.state, doc.address.city,address.country], doc);
查看byCityStateCountry":
view "byCityStateCountry":
emit([address.city,doc.address.state, doc.address.country], doc);
只是在第一个位置放一个标志来确定查询的类型,这样你就可以在一个视图中完成所有操作:
of just put a flag in the first place to determine the type of query, so you can do all in one View:
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",{}]
这篇关于couchdb 搜索或过滤键数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!