0

I am using mongo 4.2 version. I have created a sharded collection. I want to update the values of few fields which include sharded keys. As per the mongo document https://docs.mongodb.com/manual/core/sharding-shard-key/#change-a-document-s-shard-key-value .It says that we can update the value of shard keys. But I am getting the following error

Performing an update on the path 'status' would modify the immutable field 'status'

Please let me know what am I missing here

I tried the below query

.updateOne({date:{'$gte': '2021-01-01', '$lte': '2021-01-15'}, 
status: 'U' ,
type: 'SC',
name: 'product1',
currency: 'INR' },
{$set: {status: 'M'}})

This is the shard keys

 shard key: {
                        "date" : 1,
                        "status" : 1,
                        "type" : 1,
                        "name" : 1,
                        "currency" : 1
                        }
user1187
  • 2,116
  • 8
  • 41
  • 74

1 Answers1

0

First of all ensure you are not running the database in feature compatibility mode with a previous version db.adminCommand( { setFeatureCompatibilityVersion: "4.2" } ). Shard keys were immutable before v4.2.

Then ensure there are no documents where "status" is part of the compound primary key _id: .find( { "_id.status": {$exists: true} } ). _id is still immutable regardless of sharding and it's one of the reasons to use ObjectID for primary keys.

Alex Blex
  • 34,704
  • 7
  • 48
  • 75