异常信息
repl_test:PRIMARY> db.0917order_totals_b.find()
2018-09-28T15:13:03.992+0800 E QUERY [main] SyntaxError: identifier starts immediately after numeric literal @(shell):1:2
repl_test:PRIMARY> db.0917order_totals_b.find({})
2018-09-28T15:13:13.464+0800 E QUERY [main] SyntaxError: identifier starts immediately after numeric literal @(shell):1:2
情景还原
【使用mapReduce统计指定时间戳区间的交易行为总数】
聚合,统计0917消费金额总和
Aggregation — MongoDB Manual https://docs.mongodb.com/manual/aggregation/
db.accounting_center_orders.mapReduce(function(){emit(this.service_id,this.val)},function(key,values){return Array.sum(values)},{query:{create_timestamp:{$lt:1537200000,$gt:1537113600}},out:"0917order_totals"})
{
"result" : "0917order_totals",
"timeMillis" : 978,
"counts" : {
"input" : 78010,
"emit" : 78010,
"reduce" : 2828,
"output" : 14
},
"ok" : 1
}
结果新建集合
repl_test:PRIMARY> show collections
0917order_totals
Analyse_consume
db.accounting_center_orders.mapReduce(function(){emit(this.service_id,this.val)},function(key,values){return Array.sum(values)},{query:{create_timestamp:{$lt:1537200000,$gt:1537113600}}})
mapReduce 需要指明结果的输出集合名
repl_test:PRIMARY> db.accounting_center_orders.mapReduce(function(){emit(this.service_id,this.val)},function(key,values){return Array.sum(values)},{query:{create_timestamp:{$lt:1537200000,$gt:1537113600}}})
2018-09-28T15:00:07.163+0800 E QUERY [main] TypeError: c.out is undefined :
DBCollection.prototype.mapReduce@src/mongo/shell/collection.js:1401:1
@(shell):1:1
查询其他集合返回预期信息
repl_test:PRIMARY> db.0917order_totals_b.find()
2018-09-28T15:16:25.627+0800 E QUERY [main] SyntaxError: identifier starts immediately after numeric literal @(shell):1:2
2节点主备复制集
[root@e ~]# cat /usr/local/mongodb/conf/mongod.conf
bind_ip=10.14.14.12
port=27017
dbpath=/mnt/mongodb_data/data
logpath=/mnt/mongodb_data/log/mongod.log
pidfilepath=/usr/local/mongodb/mongo.pid
fork=true
logappend=true
shardsvr=true
directoryperdb=true
replSet=repl_test
keyFile=/usr/local/mongodb/conf/keyFile
[root@e ~]#
删除集合报同样错误
repl_test:PRIMARY> db.0917order_totals.drop()
2018-09-28T15:27:20.825+0800 E QUERY [main] SyntaxError: identifier starts immediately after numeric literal @(shell):1:2
repl_test:PRIMARY>
变换命名
repl_test:PRIMARY> db.accounting_center_orders.mapReduce(function(){emit(this.service_id,this.val)},function(key,values){return Array.sum(values)},{query:{create_timestamp:{$lt:1537200000,$gt:1537113600}},out:"order_totalsMR"})
{
"result" : "order_totalsMR",
"timeMillis" : 905,
"counts" : {
"input" : 78010,
"emit" : 78010,
"reduce" : 2828,
"output" : 14
},
"ok" : 1
}
repl_test:PRIMARY> show collections
order_totalsMR
repl_test:PRIMARY> db.order_totalsMR.find()
{ "_id" : 0, "value" : 2472975 }
{ "_id" : 1, "value" : -316177.25 }
{ "_id" : 2, "value" : -211655 }
{ "_id" : 4, "value" : -189450 }
{ "_id" : 7, "value" : -7750 }
{ "_id" : 8, "value" : -194385 }
{ "_id" : 10, "value" : -10000 }
{ "_id" : 11, "value" : -184954.40000000005 }
{ "_id" : 13, "value" : -35050 }
{ "_id" : 16, "value" : -30000 }
{ "_id" : 19, "value" : -6000 }
{ "_id" : 20, "value" : -1329.9999999999998 }
{ "_id" : 21, "value" : -361475 }
{ "_id" : 22, "value" : -600 }
repl_test:PRIMARY>
得到预期输入
【 集合命名规范 :英文字母开头】