MongoDB 数据集:对不减少或脚本有问题

MongoDB dataset : pairs not reducing or problem with script

我是编程新手,mongoDB 正在学习,我正在尝试使用 mongoDB mapreduce on a dataset。到目前为止,我已经将 csv 转换为 json 并使用指南针将其导入 mongoDB。

Compass 中的数据现在如下所示:

_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" } ]

我已经删除了上面的内容,现在我正在执行从 link 到 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
}

我添加了 "key value pairs" 但我似乎无法从数据中获取任何信息。

> 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"
    }
)

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

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

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