[SERVER-9215] Can't use $setOnInsert with a shard key Created: 02/Apr/13  Updated: 01/Mar/17  Resolved: 03/Apr/13

Status: Closed
Project: Core Server
Component/s: Sharding
Affects Version/s: 2.4.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Esteban Feldman Assignee: Alberto Lerner
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Linux Ubuntu 12.10


Operating System: ALL
Steps To Reproduce:

mongos> db.total_video_views.stats()
{
        "sharded" : true,
        "ns" : "vizvid.total_video_views",
        "count" : 0,
        "numExtents" : 6,
        "size" : 0,
        "storageSize" : 11182080,
        "totalIndexSize" : 24528,
        "indexSizes" : {
                "_id_" : 8176,
                "card_1" : 8176,
                "site_1_vid_1_ts_1" : 8176
        },
        "avgObjSize" : 0,
        "nindexes" : 3,
        "nchunks" : 1,
        "shards" : {
                "shard0000" : {
                        "ns" : "vizvid.total_video_views",
                        "count" : 0,
                        "size" : 0,
                        "storageSize" : 11182080,
                        "numExtents" : 6,
                        "nindexes" : 3,
                        "lastExtentSize" : 8388608,
                        "paddingFactor" : 1.9940000000003262,
                        "systemFlags" : 1,
                        "userFlags" : 0,
                        "totalIndexSize" : 24528,
                        "indexSizes" : {
                                "_id_" : 8176,
                                "site_1_vid_1_ts_1" : 8176,
                                "card_1" : 8176
                        },
                        "ok" : 1
                }
        },
        "ok" : 1
}
mongos> db.total_video_views.count()
0
mongos> db.total_video_views.update({'site': 1, 'vid': 1, 'ts': 1}, {'$set': {'count': 1}, '$setOnInsert': {'card': 1}}, {upsert:true})
Can't modify shard key's value. field: card: 1.0 collection: vizvid.total_video_views

Participants:

 Description   

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



 Comments   
Comment by Akira Kurogane [ 01/Mar/17 ]

For ticket search reasons: please note that the error string "does not contain shard key for pattern" in 3.2 (or lower?) is replaced by "An upsert on a sharded collection must contain the shard key and have the simple collation" in 3.4. The context in this ticket applies to it so long as it is just a shard key issue and collation is absent on the collection or otherwise irrelevant.

Comment by Eliot Horowitz (Inactive) [ 03/Apr/13 ]

This is 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.
You can add it to the query portion.

Generated at Thu Feb 08 03:19:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.