CouchDB Querying Dates

百般思念 提交于 2019-12-12 05:42:22

问题


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?

Map Function:

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);
	}
}

Reduce Function:

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]
}

Query All Rooms:

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]

Query Result:

{"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]}
]}

Query All Rooms:

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]

Query Result:

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

回答1:


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.

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);

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. `



来源:https://stackoverflow.com/questions/32728837/couchdb-querying-dates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!