问题
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