我是编程和mongoDB的新手,我正在学习,我正在尝试使用mongoDB在mapreduce上使用dataset。到目前为止,我已经将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 “密钥”:{ “ _id”:1 }, “ name”:“ id ”, “ ns”:“ myCrypto.testmyCrypto” }, { “ v”:2 “密钥”:{ “子弹”:1 }, “ name”:“ slug_1”, “ ns”:“ myCrypto.testmyCrypto” }, { “ v”:2 “密钥”:{ “符号”:2 }, “ name”:“ symbol_2”, “ ns”:“ myCrypto.testmyCrypto” }, { “ v”:2 “密钥”:{ “名称”:3 }, “ name”:“ name_3”, “ ns”:“ myCrypto.testmyCrypto” }, { “ v”:2 “密钥”:{ “数据”:4 }, “ name”:“ data_4”, “ ns”:“ myCrypto.testmyCrypto” }, { “ v”:2 “密钥”:{ “ ranknow”:4 }, “ name”:“ ranknow_4”, “ ns”:“ myCrypto.testmyCrypto” }, { “ v”:2 “密钥”:{ “ ranknow”:5 }, “ name”:“ ranknow_5”, “ ns”:“ myCrypto.testmyCrypto” }, { “ v”:2 “密钥”:{ “开放”:6 }, “ name”:“ open_6”, “ ns”:“ myCrypto.testmyCrypto” }, { “ v”:2 “密钥”:{ “高”:7 }, “ name”:“ high_7”, “ ns”:“ myCrypto.testmyCrypto” }, { “ v”:2 “密钥”:{ “低”:8 }, “ name”:“ low_8”, “ ns”:“ myCrypto.testmyCrypto” }, { “ v”:2 “密钥”:{ “体积”:9 }, “ name”:“ volume_9”, “ ns”:“ myCrypto.testmyCrypto” }, { “ v”:2 “密钥”:{ “市场”:10 }, “名称”:“ market_10”, “ ns”:“ myCrypto.testmyCrypto” }, { “ v”:2 “密钥”:{ “ close_ratio”:11 }, “ name”:“ close_ratio_11”, “ ns”:“ myCrypto.testmyCrypto” }, { “ v”:2 “密钥”:{ “传播”: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" } )
{ “结果”:“日期04-28”, “ timeMillis”:816,
"counts" : { "input" : 0, "emit" : 0, "reduce" : 0, "output" : 0 }, "ok" : 1 }
>
答案 0 :(得分:4)
如果您尝试对某些值求和,则它们需要为数字(将数据导入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"
}
)
此查询将为您返回每个按日期过滤的段的打开值的总和。
ps。您可以通过聚合来做同样的事情。
如果您有任何问题,请告诉我。