CouchDB查询日期 [英] CouchDB Querying Dates

查看:91
本文介绍了CouchDB查询日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在CouchDB中收集温度,并想按房间,年,月,日,小时查询平均温度.不幸的是,当我执行查询时(见下文),我得到的是所有月份,而不仅仅是我在查询中指定的月份.但是,如果仅指定一个房间,则只能获得在查询中指定的月份.如何查询指定期间内的所有房间?

I'm collecting temperatures in CouchDB and would like to query for the average temperature by room, year, month, day, hour. Unfortunately, when I execute my query (see below) I'm getting all months, not just the month I specify in my query. However, if I specify just a single room I get only the month I specify in my query. How do I query all rooms for just a specified period?

地图功能:

function(doc) {
  if(doc.type == "TempHumid"){
	var d = new Date(doc.datetime);
	for(var i in doc.path)
    		emit([doc.path[i], d.getFullYear(),d.getMonth() + 1,d.getDate(), d.getHours()], +doc.temp);
	}
}

减少功能:

function(keys, values, rereduce) {
    var avg, length;
    if (!rereduce){
       length = values.length
	var total = sum(values)
	avg = parseFloat(total / length).toFixed(2)
    }else{
        length = sum(values.map(function(v){return v[1]}))
        avg = parseFloat(sum(values.map(function(v){
            return v[0] * (v[1] / length)
            }))).toFixed(2)
    }
    return [avg, length]
}

查询所有房间:

http://127.0.0.1:5984/dev_data_v2/_design/views/_view/avg_temp?reduce=true&group_level=3&startkey=["a",2015,9,1,0]&endkey=["z",2015,10,1,0]

查询结果:

{"rows":[
  {"key":["Bedroom 1",2015,9],"value":["73.63",2292]},
  {"key":["Home",2015,8],"value":["75.27",1476]},
  {"key":["Home",2015,9],"value":["74.59",14859]},
  {"key":["Bedroom 2",2015,8],"value":["81.16",8]},
  {"key":["Bedroom 2",2015,9],"value":["73.88",2964]},
  {"key":["Kitchen",2015,9],"value":["74.44",3352]},
  {"key":["Main Level",2015,9],"value":["74.43",3352]},
  {"key":["Bedroom 3",2015,8],"value":["75.35",705]},
  {"key":["Bedroom 3",2015,9],"value":["75.72",3270]},
  {"key":["Office",2015,8],"value":["75.14",763]},
  {"key":["Office",2015,9],"value":["74.98",2981]},
  {"key":["Upstairs",2015,8],"value":["75.27",1476]},
  {"key":["Upstairs",2015,9],"value":["74.64",11507]}
]}

查询所有房间:

http://127.0.0.1:5984/dev_data_v2/_design/views/_view/avg_temp?reduce=true&group_level=3&startkey=["Bedroom 2",2015,9,1,0]&endkey=["Bedroom 2",2015,10,1,0]

查询结果:

{"rows":[
  {"key":["Bedroom 2",2015,9],"value":["73.88",2964]}
]}

推荐答案

这是因为您不了解ouchDB如何处理键.couchDB的键是列表中的位置(实际上是一棵树,但列表的参数相同),因此当您说要以 ["a",2015,9,1,0] <开头时,/code> couchDB从列表中的该位置开始(如果不指定任何键,则得到的结果,然后从该点向前移动,直到到达 ["z"处的结束键,2015,10,1,0] .CouchDB从左到右匹配数组,因此 ["a",foo] 总是在 ["b",bar] 对于所有foo,bar.

It's because you don't understand how couchDB handles keys. A key for couchDB is a position in a list (a tree actually but the argument is the same for lists) so when you say that you want to start at ["a",2015,9,1,0] couchDB starts at that position in the list (the result you get if you don't specify any keys, and then moves forward from that point until it gets to the endkey at ["z",2015,10,1,0]. CouchDB matches left to right for arrays, so ["a",foo] will always come before ["b",bar] for all foo, bar.

如果您希望能够在某个日期过滤结果,建议您将函数更改为`generate([d.getFullYear(),d.getMonth()+ 1,d.getDate(),d.getHours(),doc.path [i],+ doc.temp);

If you want to be able to filter results on a date I'd suggest you change your function to ` emit([d.getFullYear(),d.getMonth() + 1,d.getDate(), d.getHours(), doc.path[i]], +doc.temp);

如果您希望能够有时在房间上进行过滤,而在其他日期创建每个房间的视图,则在ouchDB中,视图确实很便宜.`

And if you want to be able to filter on rooms sometimes and dates other times create a view for each of them, views are really cheap in couchDB. `

这篇关于CouchDB查询日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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