Mongodb 具有两种不同结果的MapReduce函数
对于以下文档Mongodb 具有两种不同结果的MapReduce函数,mongodb,mapreduce,Mongodb,Mapreduce,对于以下文档 { "_id" : ObjectId("511b7d1b3daee1b1446ecdfe"), "l_linenumber" : 1, "l_quantity" : 17, "l_extendedprice" : 21168.23, "l_discount" : 0.04, "l_tax" : 0.02, "l_returnflag" : "N", "l_linestatus" : "O", "l_shipda
{
"_id" : ObjectId("511b7d1b3daee1b1446ecdfe"),
"l_linenumber" : 1,
"l_quantity" : 17,
"l_extendedprice" : 21168.23,
"l_discount" : 0.04,
"l_tax" : 0.02,
"l_returnflag" : "N",
"l_linestatus" : "O",
"l_shipdate" : ISODate("1996-03-13T03:00:00Z"),
"l_commitdate" : ISODate("1996-02-12T03:00:00Z"),
"l_receiptdate" : ISODate("1996-03-22T03:00:00Z"),
"l_shipinstruct" : "DELIVER IN PERSON",
"l_shipmode" : "TRUCK",
"l_comment" : "blithely regular ideas caj",
}
我尝试了两个类似的map reduce函数:
首先
第二
db.runCommand({
mapreduce: "lineitem",
map : function Map() {
var dataInicial = new Date("Jan 1, 1994");
var dataFinal = new Date("Jan 1, 1995");
if( this.l_discount>=0.05 && this.l_discount<=0.07 && this.l_quantity<24 && this.l_shipdate>=dataInicial && this.l_shipdate<dataFinal) {
var produto = this.l_extendedprice * this.l_discount;
emit("revenue", produto);
}
},
reduce : function(key, values) {
return Array.sum(values);
},
out: 'query006'
db.runCommand({
mapreduce:“行项目”,
映射:函数映射(){
var Datainical=新日期(“1994年1月1日”);
var dataFinal=新日期(“1995年1月1日”);
如果(this.l_折扣>=0.05&&this.l_折扣,我认为您的查询违反了以下段落:
MongoDB在指定以逗号分隔的表达式列表时提供了隐式AND操作。例如,您可以将上述查询编写为:
db.inventory.find({价格:1.99,数量:{$lt:20},销售:true})
但是,如果查询需要对同一字段(如{price:{$ne:1.99}}和{price:{$exists:true}}}
)执行AND操作,则对这两个单独的表达式使用$和
运算符,或者对{price:{$ne:1.99,$exists:true}}
字段组合运算符表达式
如果我们遵循文档的建议,下面的查询应该可以做到这一点:
query: {
l_shipdate: {'$gte': new Date("Jan 01, 1994"), '$lt': new Date("Jan 01, 1995")},
l_discount: {'$gte':0.05, '$lte':0.07},
l_quantity: {'$lt':24}
},
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;
query: {
l_shipdate: {'$gte': new Date("Jan 01, 1994"), '$lt': new Date("Jan 01, 1995")},
l_discount: {'$gte':0.05, '$lte':0.07},
l_quantity: {'$lt':24}
},