Details
Description
Upserts require enforcing a uniqueness constraint via a query - in a sharded cluster the only way to do this is if the upsert query is targeted by the shard key. The error message "does not contain shard key", however, is a bit vague in that the shard key may be included in the update expression of the update, but the query also requires it.
Original description:
2.6 behavior:
mongos> sh.shardCollection("test.f1",{a:1})
|
{ "collectionsharded" : "test.f1", "ok" : 1 }
|
mongos> db.f1.update({x:1},{$setOnInsert:{a:1}},{upsert:true})
|
WriteResult({
|
"nMatched" : 0,
|
"nUpserted" : 0,
|
"nModified" : 0,
|
"writeError" : {
|
"code" : 61,
|
"errmsg" : "upsert { q: { x: 1.0 }, u: { $setOnInsert: { a: 1.0 } }, multi: false, upsert: true } does not contain shard key for pattern { a: 1.0 }"
|
}
|
})
|
2.4 behavior:
mongos> db.f1.update({x:1},{$setOnInsert:{a:1}},{upsert:true})
|
Cannot use commands write mode, degrading to compatibility mode
|
WriteResult({
|
"nMatched" : 0,
|
"nUpserted" : 0,
|
"writeError" : {
|
"code" : 13123,
|
"errmsg" : "Can't modify shard key's value. field: a: 1.0 collection: test.f1"
|
}
|
})
|