gpt4 book ai didi

MongoDB 映射归约函数语法

转载 作者:可可西里 更新时间:2023-11-01 10:05:51 26 4
gpt4 key购买 nike

好吧,我正在尝试将此 sql 查询转换为 map reduce

select
o_orderpriority,
count(*) as order_count
from
orders
where
o_orderdate >= date '1993-07-01'
and o_orderdate < date '1993-07-01' + interval '3' month
and exists (
select
*
from
lineitem
where
l_orderkey = o_orderkey
and l_commitdate < l_receiptdate
)
group by
o_orderpriority
order by
o_orderpriority;

我尝试了以下 map reduce 函数

    db.runCommand({
mapreduce: "orders",
query: {
o_orderdate: {'$gte': new Date("July 01, 1993")},
o_orderdate: {'$lt': new Date("Oct 01, 1993")}
},
map: function Map() {
for(var i in this.o_lineitem) {
if( this.o_lineitem[i].l_commitdate < this.o_lineitem[i].l_receiptdate) {
var o_orderpriority = this.o_lineitem[i].o_orderpriority;
emit( o_orderpriority, {count: 1} );
}
}
},
reduce: function(key, values) {
var count= 0;
for (var i = 0; i < values.length; i++) {
count+= values[i];
}
return count;
},
out: 'query004'
});

当我运行时,我得到跟随警报

Sat Aug 11 20:44:32 SyntaxError: missing ) after condition (shell):9

对我来说没有 ) 丢失,是吗?

我做了@Stenie 指出的更正,但现在我遇到了以下问题

{
"assertion" : "value too large to reduce",
"assertionCode" : 13070,
"errmsg" : "db assertion failure",
"ok" : 0
}

最佳答案

问题是你的 emit 和你的 reduce 函数不返回相同的东西。

您的 map 函数发出值:

{count: 1}

这意味着您的 reduce 必须返回相同的格式。

您在 reduce 中返回了一个简单的值:

return count;

您可以将您的发射更改为仅发射 1 而不是 JSON 文档,然后您不必更改您的 reduce,否则更改您的 reduce 以返回一个 JSON 文档 {count: X},其中 X 是计算的计数。

仅供引用,这导致错误“值太大而无法减少”的原因是,一旦您像这样混合类型,“+”运算符就会开始连接您的值而不是添加它们,最终它会变得太大。要查看如何调试它,我推荐 Troubleshooting MapReduce

关于MongoDB 映射归约函数语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11918595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com