【问题标题】:CouchDB View with Multiple Parameters具有多个参数的 CouchDB 视图
【发布时间】:2015-10-02 00:12:40
【问题描述】:

我有一个包含 id 列和 ts 列的数据库。我需要能够传入 id 以及开始时间和结束时间来检索指定时间段内的所有值。我可以使用视图执行此操作,还是需要视图返回与 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"}}
...

...我当前的查询是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&start=1437963300000&end=1437966000000,但不知道该怎么做。

【问题讨论】:

    标签: views couchdb


    【解决方案1】:

    编辑: 为了让 Couch 匹配您的查询,所有数据(例如:A-Meter-KW 和日期)必须在视图发出的 key 中。所以我认为你可能会这样做:

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

    然后就可以使用参数startkeyendkey对结果进行适当的过滤了。

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

    附带考虑:我不会在我的文档中使用“id”属性,因为它很容易与“_id”(强制)混淆。

    【讨论】:

    • 欢迎来到 Stack Overflow!虽然这可能会回答问题,it would be preferable 在此处包含答案的基本部分,并提供链接以供参考。
    • 感谢您的建议,此编辑是否适合 @IKavanagh?
    • 看起来它可能有效,所以我投了赞成票。但是,我无法在插入时找到解决方案,因此将我的历史数据移动到 sqlite 数据库中。我所有基于文档的数据仍在 CouchDB 中运行,但对于时间序列、历史数据来说,这似乎不是一个很好的解决方案。因此,我无法验证此解决方案是否可以解决我的问题。那是不久前的事了,但我似乎记得尝试过类似的事情并且遇到了递归视图的问题,或者类似的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多