[SERVER-4539] erroneous error message "valid shard key must be in update object for collection" when updating sharded collection Created: 21/Dec/11  Updated: 19/Apr/12  Resolved: 23/Dec/11

Status: Closed
Project: Core Server
Component/s: Sharding
Affects Version/s: 1.8.2, 2.0.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Joseph Shraibman Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Fedora 16 x86_64


Operating System: ALL
Participants:

 Description   

On an unsharded database you can update a document without having to pass in the _id as a value in the update document. The new document will retain the old _id. For sharded collections if you want to update a document you have to include the _id (or whatever your shard key is) or you will get an error.

use admin
db.runCommand(

{"enablesharding" : "test"}

);
db.runCommand( { shardcollection : "test.coll", key : {_id : 1} } );

use test
db.coll.insert(

{ "_id": "theid", "num" : 1}

)
db.coll.update(

{ "_id": "theid"}

,

{"num" : 2}

)

results in:

valid shard key must be in update object for collection: test.coll



 Comments   
Comment by Lynn Hsu [ 19/Apr/12 ]

I saw Joseph's case above.

Looks like he already included a valid shard key in the upsert, why it still failed?

mongos> db.foo.update(

{ _id : 1 }

,

{ x : 3 }

, true )
can't upsert something without valid shard key

Thanks

Comment by Joseph Shraibman [ 23/Dec/11 ]

That's not what happens for me. On an unsharded collection:

> db.foo.insert({ "_id" : 1, x : 2});
> db.foo.find()
{ "_id" : 1, "x" : 2 }
> db.foo.update( { _id : 1 } , { x : 1 } )
> db.foo.find()
{ "_id" : 1, "x" : 1 }
> db.foo.update( { _id : 1 } , { x : 3 }, true )
> db.foo.find()
{ "_id" : 1, "x" : 3 }

On a sharded collection:

mongos> db.foo.insert({ "_id" : 1, x : 2});
mongos> db.foo.find()
{ "_id" : 1, "x" : 2 }
mongos> db.foo.update( { _id : 1 } , { x : 1 } )
valid shard key must be in update object for collection: test.foo
mongos> db.foo.update( { _id : 1 } , { x : 3 }, true )
can't upsert something without valid shard key

Comment by Eliot Horowitz (Inactive) [ 23/Dec/11 ]

This is by design.
See:

> db.foo.update( { _id : 1 } , { x : 1 } , true )
> db.foo.findOne()
{ "_id" : ObjectId("4ef415f8ea2cc2db11494099"), "x" : 1 }

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