$setOnInsert with a shard key – not possible ?

Having the collection empty, when trying to do an upsert using $setOnInsert on a shard key I get the following error:
Can’t modify shard key’s value. field: card: 1.0 collection: vizvid.total_video_views

mongos> db.dbversity.status()
{
“sharded” : true,
“ns” : “dbfry.dbversity”,
“count” : 0,
“numExtenid3” : 6,
“size” : 0,
“storageSize” : 11182080,
“totalIndexSize” : 24528,
“indexSizes” : {
“_id_” : 8176,
“id4_1” : 8176,
“id1_1_id2_1_id3_1” : 8176
},
“avgObjSize” : 0,
“nindexes” : 3,
“nchunks” : 1,
“shards” : {
“shard0000” : {
“ns” : “dbfry.dbversity”,
“count” : 0,
“size” : 0,
“storageSize” : 11182080,
“numExtenid3” : 6,
“nindexes” : 3,
“lastExtenid3ize” : 8388608,
“paddingFactor” : 1.9940000000003262,
“systemFlags” : 1,
“userFlags” : 0,
“totalIndexSize” : 24528,
“indexSizes” : {
“_id_” : 8176,
“id1_1_id2_1_id3_1” : 8176,
“id4_1” : 8176
},
“ok” : 1
}
},
“ok” : 1
}
mongos> db.dbversity.count()
0
mongos> db.dbversity.update({‘id1’: 1, ‘id2’: 1, ‘id3’: 1}, {‘$set’: {‘count’: 1}, ‘$setOnInsert’: {‘id4’: 1}}, {upsert:true})
Can’t modify shard key’s value. field: id4: 1.0 collection: dbfry.dbversity

Seems it’s by design, on an update where the query doesn’t contain the shard key, we don’t know which shard it would hit, and therefore can’t set the shard key.
However, we can add it to the query portion.

  • Ask Question