couchdb search or filtering on key array

后端 未结 1 1871
情深已故
情深已故 2020-12-07 14:36

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 h

相关标签:
1条回答
  • 2020-12-07 15:27

    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",{}]
    
    0 讨论(0)
提交回复
热议问题