gpt4 book ai didi

MongoDB 数据集 : pairs not reducing or problem with script

转载 作者:可可西里 更新时间:2023-11-01 09:12:43 29 4
gpt4 key购买 nike

我是编程和 mongoDB 的新手,正在学习,我正在尝试 mapreducedataset 上使用 mongoDB。到目前为止,我已经将 csv 转换为 json 并使用罗盘将其导入到 mongoDB 中。

在罗盘中,数据现在看起来像这样:

_id     :5bc4e11789f799178470be53
slug :"bitcoin"
symbol :"BTC"
name :"Bitcoin"
date :"2013-04-28"
ranknow :"1"
open :"135.3"
high :"135.98"
low :"132.1"
close :"134.21"
volume :"0"
market :"1500520000"
close_ratio :"0.5438"
spread :"3.88"

我已将每个值添加为索引,如下所示,这是正确的过程以便我可以对数据运行 mapreduce 吗?

db.testmyCrypto.getIndices() [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "id", "ns" : "myCrypto.testmyCrypto" }, { "v" : 2, "key" : { "slug" : 1 }, "name" : "slug_1", "ns" : "myCrypto.testmyCrypto" }, { "v" : 2, "key" : { "symbol" : 2 }, "name" : "symbol_2", "ns" : "myCrypto.testmyCrypto" }, { "v" : 2, "key" : { "name" : 3 }, "name" : "name_3", "ns" : "myCrypto.testmyCrypto" }, { "v" : 2, "key" : { "data" : 4 }, "name" : "data_4", "ns" : "myCrypto.testmyCrypto" }, { "v" : 2, "key" : { "ranknow" : 4 }, "name" : "ranknow_4", "ns" : "myCrypto.testmyCrypto" }, { "v" : 2, "key" : { "ranknow" : 5 }, "name" : "ranknow_5", "ns" : "myCrypto.testmyCrypto" }, { "v" : 2, "key" : { "open" : 6 }, "name" : "open_6", "ns" : "myCrypto.testmyCrypto" }, { "v" : 2, "key" : { "high" : 7 }, "name" : "high_7", "ns" : "myCrypto.testmyCrypto" }, { "v" : 2, "key" : { "low" : 8 }, "name" : "low_8", "ns" : "myCrypto.testmyCrypto" }, { "v" : 2, "key" : { "volume" : 9 }, "name" : "volume_9", "ns" : "myCrypto.testmyCrypto" }, { "v" : 2, "key" : { "market" : 10 }, "name" : "market_10", "ns" : "myCrypto.testmyCrypto" }, { "v" : 2, "key" : { "close_ratio" : 11 }, "name" : "close_ratio_11", "ns" : "myCrypto.testmyCrypto" }, { "v" : 2, "key" : { "spread" : 13 }, "name" : "spread_13", "ns" : "myCrypto.testmyCrypto" } ]

我已经抓取了上面的内容,现在我正在从指向 map-reduce 的链接执行以下操作。这是正确的输出吗?

> db.testmyCrypto.mapReduce(function() { emit( this.slug, this.symbol ); }, function(key, values) { return Array.sum( values ) },
... {
... query: { date:"2013-04-28" },
... out: "Date 04-28"
... }
... )
{
"result" : "Date 04-28",
"timeMillis" : 837,
"counts" : {
"input" : 0,
"emit" : 0,
"reduce" : 0,
"output" : 0
},
"ok" : 1
}

我已经添加了“键值对”,但我似乎无法从数据中获取任何信息。

> db.testmyCrypto.mapReduce(function() { emit( this.slug, this.symbol, this.name, this.date, this.ranknow, this.open, this.high, this.low, this.close, this.volume, this.market, this.close_ratio, this.spread ); }, function(key, values) { return Array.sum( values ) }, { query: { slug:"bitcoin" }, out: "Date 04-28" } )

{ "result" : "Date 04-28", "timeMillis" : 816,

"counts" : {
"input" : 0,
"emit" : 0,
"reduce" : 0,
"output" : 0
},
"ok" : 1 }

>

最佳答案

如果您尝试对某些值求和,则它们需要为数字(当您将数据导入 mongo 时尝试设置值的类型)

db.collectionName.mapReduce(
function() {
emit(
this.slug,
this.open
)
},
function(keySlug, valueOpen) {
return Array.sum(valueOpen)
},
{
query: { date:"2013-04-28" },
out: "Date 04-28"
}
)

此查询将为您返回按日期过滤的每个 slug 的开放值总和。

附言。你可以用聚合做同样的事情。

如果你有任何问题,请告诉我。

关于MongoDB 数据集 : pairs not reducing or problem with script,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52910397/

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