与范围条件汇总

时间:2017-06-03 10:36:00

标签: mongodb aggregation-framework

我有一个包含两种类型文档的集合:

{"_id" : 172347106,"name" : "product 1", "type" : "product"}
{"_id" : 135377126,"name" : "product 2", "type" : "product"}
{"_id" : 135378126,"name" : "product 3", "type" : "product"}
{"price" : 8,"id_start" : 172347000,"id_end" : 173347106, "type" : "price"}
{"price" : 6,"id_start" : 135377000,"id_end" : 136377126, "type" : "price"}

我想要第二个系列只有价格的产品(价格可以在id_start和id_end之间找到ID),如:

{"_id" : 172347106,"name" : "product 1", "price" : 8}
{"_id" : 135377126,"name" : "product 2", "price" : 6}
{"_id" : 135378126,"name" : "product 3", "price" : 6}

当文档在separates集合中时,我执行以下查询:

db.getCollection("product").find({}).forEach( function(myProduct) {
     db.getCollection("price").findOne({$and: [ { "id_start":{$lt: parseInt(myProduct._id) } }, {"id_end":{$gt: parseInt(myProduct._id)}}]})
});

如何使用Aggregation框架? (或者可能是map / reduce?)

非常感谢!

0 个答案:

没有答案