0

I'm somewhat new to MongoDB and I'm trying to change a primary key on a record via PHP. When I try to do so, I get the following error:

PHP Fatal error: Uncaught exception 'MongoWriteConcernException' with message '12.34.56.78:27017: After applying the update to the document {_id: ObjectId('5b077638aaf909b2528b51fd') , ...}, the (immutable) field '_id' was found to have been altered to _id: ObjectId('58b5882c4658a3de67ce2907')'

The new id (58b5882c4658a3de67ce2907) has been confirmed as unique.

This is the PHP code that I'm using to attempt this change:

$contactsQuery_for_update = array('center' => 12345,'_id' => new 
MongoId('5b077638aaf909b2528b51fd'));   
$collection_contacts->update(
$contactsQuery_for_update,
[ '$set' => [ '_id' => new MongoId('58b5882c4658a3de67ce2907') ]]
);

Has anyone else been able to do this successfully? Any direction is appreciated. Thanks!

1 Answers1

1

I think the error message answers your question by telling you it's immutable. i.e. it can't be changed.

I guess if you have good reason to change the field, you'd have to delete the old record, and insert a new one with your preferred id.

Tim
  • 8,036
  • 2
  • 36
  • 52