关键阵列CouchDB的搜索和过滤 [英] couchdb search or filtering on key array

查看:102
本文介绍了关键阵列CouchDB的搜索和过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个在我看来功能:

 发出([doc.address.country,doc.address.state,doc.address.city] DOC);

当我查询的搜索,我需要填补数组中的所有3个元素,例如:

 ?键= [美国,纽约,纽约]

这会产生我的记录,还是让说,例如,我只是想在我们回到一切,例如:

 ?关键=US]

或在美国和国...

 ?键= [美国,纽约]

或者......可以说,也许是我想从纽约刚刚的所有记录...(我知道下面不工作)

 ?键= [,,NY]

我真的不明白如何,如果你想离开阵列的空元素之一搜索?


解决方案

第一:

键=US]将不会在一个数组键[美国,纽约]的工作,因为你正在寻找一个关键是精确[美国。相反,你必须使用

  startkey =US&放大器; endkey =US,{}]

那么这些键是在结果:

  [DE,巴伐利亚,慕尼黑]< ---- NO! DE是出于startkey的范围
[美国,FL,迈阿密]< ----是的,开始以美
[美国,纽约,纽约< ----是的,开始以美
[VE,XX,Vencuela城< ---- NO! VE已经endkey的范围

也在努力:

  startkey = [美国,FL&放大器; endkey = [美国,FL,{}]

结果:

  [DE,巴伐利亚,慕尼黑]< ---- NO! DE是出于startkey的范围
[美国,FL,迈阿密]< ----是的,开始以美国,FL
[美国,纽约,纽约< ---- NO,美国,纽约已经endkey的范围
[VE,XX,Vencuela城< ---- NO! VE已经endkey的范围

二:
你不能对左侧空白..所以你必须写一些发出:
(您不必发出第二个和第三个数组项,如果你不需要进行查询)

查看byStateCityCountry

 发出([doc.address.state,doc.address.city,address.country] DOC);

查看byCityStateCountry

 发出([address.city,doc.address.state,doc.address.country] DOC);

的只是把一个标志摆在首位,以确定查询的类型,所以你可以在一个视图:

 发出([1,address.country,doc.address.state,doc.address.city] DOC);
发射([2,doc.address.state,doc.address.city,address.country] DOC);
发出([3,address.city,doc.address.state,doc.address.country] DOC);

用法:

<?pre> startkey = [1,US&放大器; endkey = [1,美国,{}]
?startkey = [2,FL]放大器; endkey = [2,FL,{}]
?startkey = [3,迈阿密]&安培; endkey = [3,迈阿密,{}]

I have this in my view function:

emit([doc.address.country,doc.address.state, doc.address.city], doc);

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?

解决方案

First:

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

Also Working:

startkey=["US","FL"]&endkey=["US","FL",{}] 

result:

["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)

view "byStateCityCountry":

emit([doc.address.state, doc.address.city,address.country], doc);

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);

Usage:

?startkey=[1,"US"]&endkey=[1,"US",{}]
?startkey=[2,"FL"]&endkey=[2,"FL",{}]
?startkey=[3,"Miami"]&endkey=[3,"Miami",{}]

这篇关于关键阵列CouchDB的搜索和过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆