Can not update document without sharding key

1,454 views
Skip to first unread message

Lei Chen

unread,
May 9, 2012, 12:26:22 AM5/9/12
to mongodb-user
I have a collection samples sharded with key {a: 1, b: 1} and when I
do the operations below. I get an error:
mongos> db.samples.find({_id: ObjectId('4fa9d834da02534728000586')})
...
mongos> db.samples.update({_id: ObjectId('4fa9d834da02534728000586')},
{$set: {deleted: 'true'}})
right object doesn't have full shard key
...
And below operation is Ok
mongos> db.samples.update({_id: ObjectId('4fa9d834da02534728000586'),
a: 23, b: 99}, {$set: {deleted: 'true'}})

I notice that in http://www.mongodb.org/display/DOCS/Sharding+Introduction#ShardingIntroduction-OperationTypes,
it can update successfully without sharding key
db.foo.update( { x : 100 }, <object> )
db.foo.remove( { x : 100 } )
Targeted
db.foo.update( { age : 40 }, <object> )
db.foo.remove( { age : 40 } )
Global
Why? Can mongodb not support it? My Mongodb version is 2.0.4

Spencer T Brody

unread,
May 9, 2012, 11:24:19 AM5/9/12
to mongod...@googlegroups.com
Updates that don't include the shard key must be multi-updates.  Since the update will be broadcast to all shards, the mongos has no way to ensure that only one document is updated.  All updates are by default single updates, you must specify if you want them to be multi-updates.  In the shell you can do this by setting the fourth parameter to the update function to true.  See http://www.mongodb.org/display/DOCS/Updating#Updating-update%28%29 for more information about doing a multi-update.

I updated the documentation you linked to to make this behavior clearer.

Lei Chen

unread,
May 10, 2012, 3:50:52 AM5/10/12
to mongodb-user
Cool, thanks

On 5月9日, 下午11时24分, Spencer T Brody <spen...@10gen.com> wrote:
> Updates that don't include the shard key must be multi-updates.  Since the
> update will be broadcast to all shards, the mongos has no way to ensure
> that only one document is updated.  All updates are by default single
> updates, you must specify if you want them to be multi-updates.  In the
> shell you can do this by setting the fourth parameter to the update
> function to true.
>  Seehttp://www.mongodb.org/display/DOCS/Updating#Updating-update%28%29for
> more information about doing a multi-update.
>
> I updated the documentation you linked to to make this behavior clearer.
>
>
>
>
>
>
>
> On Wednesday, May 9, 2012 12:26:22 AM UTC-4, Lei Chen wrote:
>
> > I have a collection samples sharded with key {a: 1, b: 1} and when I
> > do the operations below. I get an error:
> > mongos> db.samples.find({_id: ObjectId('4fa9d834da02534728000586')})
> > ...
> > mongos> db.samples.update({_id: ObjectId('4fa9d834da02534728000586')},
> > {$set: {deleted: 'true'}})
> > right object doesn't have full shard key
> > ...
> > And below operation is Ok
> > mongos> db.samples.update({_id: ObjectId('4fa9d834da02534728000586'),
> > a: 23, b: 99}, {$set: {deleted: 'true'}})
>
> > I notice that in
> >http://www.mongodb.org/display/DOCS/Sharding+Introduction#ShardingInt...,
Reply all
Reply to author
Forward
0 new messages