简体   繁体   English

具有多个参数的CouchDB视图

[英]CouchDB View with Multiple Parameters

I have a database that has an id column and a ts column. 我有一个具有id列和ts列的数据库。 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. 我需要能够传递ID以及开始时间和结束时间来检索指定时间段内的所有值。 Can I do this with a view, or do I need the view to return all of the values that match the id? 我可以使用视图执行此操作,还是需要视图返回与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匹配的所有内容...

{"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 . ...我当前的查询是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. 我也想传递开始和结束时间,例如http://localhost:5984/hist/_design/hist/_view/byId?key=%22A-Meter-KW%22&descending=true&start=1437963300000&end=1437966000000但无法弄清楚该如何做。

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. 编辑:为了使Couch匹配您的查询,所有数据(例如:A-Meter-KW和日期)必须在视图发出的key中 So I think you might do something like: 因此,我认为您可能会执行以下操作:

emit([key,year,month,day],doc._id) 发出([键,年,月,日],doc._id)

Then you can use the parameters startkey and endkey to filter the results properly. 然后,您可以使用参数startkeyendkey来正确过滤结果。

Reference: http://guide.couchdb.org/draft/views.html#many 参考: 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. 侧面考虑:我不会在文档中使用“ id”属性,因为它很容易与“ _id”(强制性)混淆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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