简体   繁体   中英

CouchDB View with Multiple Parameters

I have a database that has an id column and a ts column. I need to be able to pass in the id and a start time and end time to retrieve all the values during the specified period. Can I do this with a view, or do I need the view to return all of the values that match the id? My concern is that I will be returning and parsing a lot more data than I really care about. Here is the format of my DB from my current view, which simply returns everything that matches the id...

{"id":"62db2aa3472dce80b1f2193fc21d52fd","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d52fd","_rev":"1-6aadd58f4f5dabacf6f4f638396246d0","id":"A-Meter-KW","ts":1437969600000,"tz":"New_York","val":"191kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d5100","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d5100","_rev":"1-71155153c0f03c49b02850bee5535e22","id":"A-Meter-KW","ts":1437968700000,"tz":"New_York","val":"190kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d45d7","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d45d7","_rev":"1-661511616958d45fdff3307600d2a9ed","id":"A-Meter-KW","ts":1437967800000,"tz":"New_York","val":"189kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d3c23","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d3c23","_rev":"1-4e97cfc6cb97ddc65f04efd9043b3abd","id":"A-Meter-KW","ts":1437966900000,"tz":"New_York","val":"188kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d2e35","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d2e35","_rev":"1-120298e95c9d2b4b9cdf438836b6c0c0","id":"A-Meter-KW","ts":1437966000000,"tz":"New_York","val":"187kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d22b0","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d22b0","_rev":"1-61e55d02bd8f0c601274b904f46c9f34","id":"A-Meter-KW","ts":1437965100000,"tz":"New_York","val":"186kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d1ce2","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d1ce2","_rev":"1-b4fe80563c70a40981e293af9c6a87b3","id":"A-Meter-KW","ts":1437964200000,"tz":"New_York","val":"185kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d1ccc","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d1ccc","_rev":"1-bdf1881c4270e68e7a7ed90a1d945228","id":"A-Meter-KW","ts":1437963300000,"tz":"New_York","val":"184kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d1303","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d1303","_rev":"1-404d5934fc882aa36e6d355d9a3485ae","id":"A-Meter-KW","ts":1437962400000,"tz":"New_York","val":"183kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d0941","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d0941","_rev":"1-64288d1c98e9b93aa6c546acb1e02078","id":"A-Meter-KW","ts":1437961500000,"tz":"New_York","val":"182kW"}}
...

... my current query is http://localhost:5984/hist/_design/hist/_view/byId?key=%22A-Meter-KW%22&descending=true . I'd like to passing a start and end time as well, something like http://localhost:5984/hist/_design/hist/_view/byId?key=%22A-Meter-KW%22&descending=true&start=1437963300000&end=1437966000000 but cannot figure out how to do this.

EDITED: In order for Couch to match your query all the data (eg:A-Meter-KW and date) must be in the key , emitted by the view. So I think you might do something like:

emit([key,year,month,day],doc._id)

Then you can use the parameters startkey and endkey to filter the results properly.

Reference: http://guide.couchdb.org/draft/views.html#many

Side consideration: I would not use an "id" property inside my documents, because it could get easily confused with the "_id" (compulsory) one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM