In our db init script, we have the following lines:
var conn = new Mongo();
var admin = conn.getDB("admin");
var db = conn.getDB("foodb");
admin.runCommand({ "enableSharding": "foodb" });
// ...
db.runCommand({ "create": "foo_stats" });
admin.runCommand({ "shardCollection": "foodb.foo_stats", key: { "_id": "hashed" } });
Then, when I try to perform a mapReduce()
on another collection and write to foo_stats
, the following happens (this is a simplified example which we actually tried after the "real" one failed):
mongos> db.foo_data.mapReduce(function(){ emit(this.foo_id, null)}, function(key, values) { return {};}, {"out": {"replace": "foo_stats","sharded": true}});
2016-05-31T06:50:07.061-0700 E QUERY Error: map reduce failed:{
"code" : 13141,
"ok" : 0,
"errmsg" : "exception: Chunk map pointed to incorrect chunk"
}
at Error (<anonymous>)
at DBCollection.mapReduce (src/mongo/shell/collection.js:1353:15)
at (shell):1:16 at src/mongo/shell/collection.js:1353
What can be the cause of this error? In the documents from the foo_data
collection, foo_id
is a string that should theoretically match the regex: ^[A-Za-z0-9\-_]{16,48}$
Best Answer
As at MongoDB 3.2, the only supported key for a sharded output collection for Map/Reduce is the
_id
field (non-hashed).There are known issues with Map/Reduce output to a sharded collection using a hashed shard index; the two features don't play well together yet and this isn't a supported combination. The documentation currently only suggests that
_id
can be used, however there could be an explicit note that hashed indexes are not supported yet.There's a relevant Jira which you can watch/upvote: SERVER-16605: Mapreduce into sharded collection with hashed index fails.
I believe your specific chunk map error is likely due to the hashed index and sharded Map/Reduce both assuming they should create the initial chunks for an empty collection.
When you create a Shard a Collection Using a Hashed Shard Key:
With a Sharded Collection as Output, Map/Reduce also tries to create the initial chunks: