如果不满足某些条件(例如总和或计数为零),是否有办法在reduce视图中不包含结果?
我的情况有点复杂,但为了简单起见,我们假设我们将航班预订作为文件进入。我也有航班时刻表,可用座位在同一个桶中。我的地图功能看起来像这样:
function (doc, meta) {
if (meta.id.match(/^flight./)) {
emit([doc.flighno, doc.schedule], {"availableSeats", doc.availableSeats});
}
else if (meta.id.match(/^request\./) && doc.status = "pending") {
emit([doc.flighno, doc.schedule], {"requestedSeats", doc.requestedSeats});
}
}
然后地图函数将发出这些行(我使用这种人类可读格式schedule
只是为了让它更容易阅读。):
"rows"=:[
{"id":"flight.123456", "key":["AC783","2016-05-04UTC"], "value":{"availableSeats":20}},
{"id":"request.1001", "key":["AC783","2016-05-04UTC"], "value":{"requestedSeats":2} },
{"id":"request.1002", "key":["AC783","2016-05-04UTC"], "value":{"requestedSeats":3} },
{"id":"request.1003", "key":["AC783","2016-05-04UTC"], "value":{"requestedSeats":5} },
{"id":"flight.654321", "key":["AC783","2016-05-11UTC"], "value":{"availableSeats":50}},
{"id":"request.2001", "key":["AC783","2016-05-11UTC"], "value":{"requestedSeats":5} },
{"id":"request.2002", "key":["AC783","2016-05-11UTC"], "value":{"requestedSeats":25}},
{"id":"request.2003", "key":["AC783","2016-05-11UTC"], "value":{"requestedSeats":35}},
{"id":"flight.999999", "key":["AC123","2016-05-13UTC"], "value":{"availableSeats":0} },
{"id":"request.3001", "key":["AC123","2016-05-13UTC"], "value":{"requestedSeats":5} },
{"id":"request.3002", "key":["AC123","2016-05-13UTC"], "value":{"requestedSeats":15}},
{"id":"request.3003", "key":["AC123","2016-05-13UTC"], "value":{"requestedSeats":35}},
{"id":"flight.888888", "key":["AC124","2016-05-13UTC"], "value":{"availableSeats":45}},
{"id":"flight.777777", "key":["AC125","2016-05-13UTC"], "value":{"availableSeats":40}},
{"id":"request.4001", "key":["AC125","2016-05-13UTC"], "value":{"requestedSeats":10}},
{"id":"request.4002", "key":["AC125","2016-05-13UTC"], "value":{"requestedSeats":10}},
{"id":"request.4003", "key":["AC125","2016-05-13UTC"], "value":{"requestedSeats":20}}
]
在我的情况下,我希望有一个reduce函数,用于汇总航班文档以及请求文档中可用席位的总和。我能得到的最远的是:
function(key, values, rereduce) {
var result = {availableSeats: 0, waiting: 0, toProcess: false};
for(var i=0; i < values.length; i++) {
if (values[i].availableSeats > 0) {
result.availableSeats += values[i].availableSeats;
}
if (values[i].requestedSeats > 0) {
result.requestedSeats += values[i].requestedSeats;
}
}
result.toProcess = Math.min(result.availableSeats, result.requestedSeats) > 0;
return result;
}
缩小视图的结果将是:
"rows"=:[
{"key":["AC783","2016-05-04UTC"], "value":{"availableSeats":20, "requestedSeats": 10, "toProcess": true}},
{"key":["AC783","2016-05-11UTC"], "value":{"availableSeats":50, "requestedSeats": 60, "toProcess": true}},
{"key":["AC123","2016-05-13UTC"], "value":{"availableSeats":0, "requestedSeats": 55, "toProcess": false}},
{"key":["AC124","2016-05-13UTC"], "value":{"availableSeats":45, "requestedSeats": 0, "toProcess": false}},
{"key":["AC125","2016-05-13UTC"], "value":{"availableSeats":40, "requestedSeats": 40, "toProcess": true}}
]
但是,基本上,我需要的是让视图只包含availableSeats
和requestedSeats
都为正的键,即toProcess
标志为真。我的大多数结果都会将其中一个值设为零,因此,与它们无关,加载整个视图(总是会增长),并不理想。
有什么建议吗?
更新:
我试着这样做:
if (result.tbd) {
return(result);
}
else {
return null;
}
以及
if (result.tbd) {
return(result);
}
else {
return {};
}
除了值null
或{}
之外,我不想要显示的键仍然显示,如下所示:
"rows"=:[
{"key":["AC783","2016-05-04UTC"], "value":{"availableSeats":20, "requestedSeats": 10, "toProcess": true}},
{"key":["AC783","2016-05-11UTC"], "value":{"availableSeats":50, "requestedSeats": 60, "toProcess": true}},
{"key":["AC123","2016-05-13UTC"], "value":null},
{"key":["AC124","2016-05-13UTC"], "value":null},
{"key":["AC125","2016-05-13UTC"], "value":{"availableSeats":40, "requestedSeats": 40, "toProcess": true}}
]
或
"rows"=:[
{"key":["AC783","2016-05-04UTC"], "value":{"availableSeats":20, "requestedSeats": 10, "toProcess": true}},
{"key":["AC783","2016-05-11UTC"], "value":{"availableSeats":50, "requestedSeats": 60, "toProcess": true}},
{"key":["AC123","2016-05-13UTC"], "value":{}},
{"key":["AC124","2016-05-13UTC"], "value":{}},
{"key":["AC125","2016-05-13UTC"], "value":{"availableSeats":40, "requestedSeats": 40, "toProcess": true}}
]
这与我最初的结果并没有太大差别,因为带有这些键的行数会增长很多。
答案 0 :(得分:0)
你已经到了一半了。您只需检查toProcess
标志并返回空结果{availableSeats: 0, waiting: 0, toProcess: false}
,如果它是假的而不是您计算的实际值。这样,您不想要的结果将不会传播到rereduce步骤,也不会被计算在内。