我一直在四处寻找并搜索互联网,但我找不到答案。也许我的汇总错了,如果我错了,请纠正我。
据说我有这样的数据
mongos> db.data_analytics.find()排序({_识别符:1})。。限制(1).pretty()
{ "title" : "data weather currency", "time" : "Sun Dec 08 2013 04:01:51 GMT+0900 (JST)", "_id" : ObjectId("52a3709f52d744c201000ba6"), "dataAnalytics" : [ { "keyItem" : "currency-yen-php", "currency" : 0.427044, "from" : "JPY", "to" : "PHP", "time" : "Sun Dec 08 2013 04:01:49 GMT+0900 (JST)", "key" : "currency" }, { "keyItem" : "weather-akiruno", "main-temp" : "5.78", "main-temp_min" : "2.78", "main-temp_max" : "7.78", "weather-main" : "Clouds", "weather-description" : "scattered clouds", "time" : "Sun Dec 08 2013 04:01:50 GMT+0900 (JST)", "key" : "weather" }, ], "__v" : 0 }
我想按“$ dataAnalytics.currency”进行分组和计数,但是我在像这样的Sharded环境中收到错误
mongos> db.data_analytics.aggregate([{$unwind: "$dataAnalytics"}, {$group:{_id:"$dataAnalytics.currency", c: {$sum:1}}}]) { "code" : 16390, "ok" : 0, "errmsg" : "exception: sharded pipeline failed on shard jeanepaul_set_pi_debian_01: { errmsg: \"exception: invalid operator \"$const\"\", code: 15999, ok: 0.0 }" }
我已经尝试在副本集中成功完成此操作,但在分片环境中没有运气。可能是什么问题?
感谢您的帮助,并提前感谢您的帮助!
答案 0 :(得分:0)
您目前正在按整个dataAnalytics
元素进行分组,您需要将.currency
(或其他一些字段)添加到_id
这样的内容:
db.data_analytics.aggregate([
{$unwind: "$dataAnalytics"},
{$group:{_id:"$dataAnalytics.currency", c: {$sum:1}}}])
答案 1 :(得分:0)
伊恩爵士,如果你能看到这个,我已经解决了这个问题。我在我的聚合中添加了一个$ match。这是我的结果
mongos> db.data_analytics.aggregate([{$unwind:"$dataAnalytics"},{$match:{"dataAnalytics.key":"currency"}},{$group:{_id:"$dataAnalytics.currency", total: {$sum:1}}}, {$limit:100}]) { "result" : [ { "_id" : 0.428345, "total" : 3 }, { "_id" : 0.428517, "total" : 7 }, { "_id" : 0.428511, "total" : 63 }, { "_id" : 0.42802, "total" : 7 } ], "ok" : 1 }
错误似乎消失了。再次感谢您的建议!