Need a CouchDB trick to sort by date and filter by group

老子叫甜甜 提交于 2019-12-10 18:14:03

问题


I have documents with fields 'date' and 'group'. And this is my view:

byDateGroup: {
  map: function(doc) {
    if (doc.date && doc.group) {
      emit([doc.date, doc.group], null);
    }
  }
}

What would be the equivalent query of this:

select * from docs where group in ("group1", "group2") order by date desc

This simple solution is not coming into my head. :(


回答1:


Pankaj, switch the order of the key you're emitting to this:

emit([doc.group, doc.date], doc);

Then you can pass in a start key and an end key when querying the view. It might be easier to do a separate query for each group that you want to pull data for. The data will be sorted by date.

I'm on my way out at the moment, but can elaborate more when I get back if it isn't clear.




回答2:


why not?

function(doc) {
  if (doc.date && ["group1", "group2"].indexOf(doc.group) !== -1)) {
    emit([doc.date, doc.group], null);
  }
}



回答3:


You need a specific view for this:

byDateGroup1Group2: {
  map: function(doc) {
    if (doc.date && doc.group && (doc.group === "group1" || doc.group === "group2") {
      emit(doc.date, doc);
    }
  }
}

that you query (presumably in a list function) with query descending=true.



来源:https://stackoverflow.com/questions/10142850/need-a-couchdb-trick-to-sort-by-date-and-filter-by-group

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