Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does: "Can't modify shard key's value fieldid for collection: foo.foo" mean in MongoDB-Perl?

Tags:

mongodb

perl

I have the foo.foo collection sharded like this:

db.runCommand( { shardcollection: "foo.foo", key: { id: 1 } } );

When I do an upsert of some document:

$connection->update(
    { id => 42 },
    { '$set' => { id => 42 } },   # using the '$set' modifier
    { upsert => 1, safe => 1 },
);

I get this exception:

Can't modify shard key's value fieldid for collection: foo.foo

If I do an upsert without modifiers:

$result = $collection->update(
    { id => $args{doc_id} },
    { id => 42 },                 # regular upsert without modifier
    { upsert => 1, safe => 1 },
);

I get this error:

cannot modify shard key for collection: foo.foo

But when I shard like this instead:

# 'id' has been changed to '_id'
db.runCommand( { shardcollection: "foo.foo", key: { _id: 1 } } );

Then when I do the same upsert as above, I get this exception:

can't upsert something without shard key

What is a "shard key's value fieldid"?

Why can I not do an upsert that sets the "id" with or without modifiers as shown in the first 2 examples?

In either case, inserts work fine. It's upserts that throw exceptions.

like image 885
Neil Avatar asked Aug 10 '11 03:08

Neil


2 Answers

Two things :

  1. Shard keys are immutable. Once you set them they cannot be updated. This makes sense because it would result in data having to be moved from one shard to another. So, update that include fields that are (or are part of) the shard key will fail with the error you're getting. The first two error messages are both the same error.

  2. You cannot make inserts into a sharded collection without the shard key. Again, this makes sense from mongo's point of view. If you're not providing a shard key how will it know to which shard it should route the insert? An upsert operation should therefore include the shard key in the find "criteria" (so, the first parameter in update(criteria, update, upsert, multi)) but not the "update" since that's the only correct way to get the shard key in an upserted document without having it in the "update" parameter which, as you noticed, is not allowed.

Make sense?

like image 183
Remon van Vliet Avatar answered Nov 15 '22 06:11

Remon van Vliet


I think the answer to this question is that there is a bug in mongodb 2.0. Check this jira issue:

https://jira.mongodb.org/browse/SERVER-3657

like image 33
Mason H. Avatar answered Nov 15 '22 06:11

Mason H.