将sql查询转换为MongoDB

时间:2012-07-05 12:53:20

标签: mongodb mapreduce nosql

嗨我新手进入MongoDB并且我需要首先在MapReduce方法和其他聚合方法中使用两种技术将此sql查询转换为mongodb。有人可以帮忙吗?

select
    sum(l_extendedprice*l_discount) as revenue
from 
    lineitem
where 
    l_shipdate >= date '1994-01-01'
    and l_shipdate < date '1994-01-01' + interval '1' year
    and l_discount between 0.06 - 0.01 and 0.06 + 0.01
    and l_quantity < 24;

1 个答案:

答案 0 :(得分:2)

http://www.mongodb.org/display/DOCS/MapReduce

对于您的样本,请使用map / reduce

var m = function () { emit(1, {this.l_extendedprice * this.l_discount})};

var r = function (k, vals) {
    var sum = 0;
    for (var i = 0; i < vals.length; i++) {
       sum += vals[i];
    }
    return sum;
}

var res = db.stuff.mapReduce(m, r,  {
                            out:"stuff_aggr",
                            query: { 
                                "l_shipdate": {$gte:  ISODate("1994-01-01T00:00:00.000Z")}, 
                                "l_shipdate": {$lte:  ISODate("1995-01-01T00:00:00.000Z")},
                                "l_discount": {$gte: 0.05}, 
                                "l_discount": {$lte: 0.07}, 
                                "l_quantity": {$lt: 24}
                            }
});

Aggregation 仍是测试版功能。 MapReduce仍然是更好的选择。我假设您想要查看是否可以轻松处理复杂的where子句......只要您将自己限制在一个集合/表中,它与SQL没有什么不同。