Skip to content
Studio 3T - The professional GUI, IDE and client for MongoDB
  • Tools
    • Aggregation Editor
    • IntelliShell
    • Visual Query Builder
    • Export Wizard
    • Import Wizard
    • Query Code
    • SQL Query
    • Connect
    • Schema Explorer
    • Compare
    • SQL ⇔ MongoDB Migration
    • Data Masking
    • Task Scheduler
    • Reschema
    • More Tools and Features
  • Solutions
  • Resources
    • Knowledge Base
    • MongoDB Tutorials & Courses
    • Tool/Feature Documentation
    • Blog
    • Community
    • Testimonials
    • Whitepapers
    • Reports
  • Contact us
    • Contact
    • Sales Support
    • Feedback and Support
    • Careers
    • About Us
  • Store
    • Buy Now
    • Preferred Resellers
    • Team Pricing
  • Download
  • My 3T
search

Why can’t I change the _id field? #Studio3T_AMA

Posted on: 19/01/2022 by Dj Walker-Morgan

Q: Why can’t I update or edit the _id field in Studio 3T?

Welcome to the latest Studio 3T Ask Manatees Anything – #Studio3T_AMA. Want to know how to use Studio 3T to make your MongoDB life better? Just tweet and use #Studio3T_AMA or email [email protected].

A: Because you can’t update the _id field in MongoDB.

Oh, yes, I’d better explain in a little more detail. First, let us talk about what the _id field is.

All about the _id field

The _id field is a very special field in MongoDB. It exists in every document in every collection and within any collection is unique. 

Make a document and if it doesn’t have an _id field, MongoDB will make one for you – using an ObjectID. ObjectID was pretty much designed to be the value in an _id field. If you do create a document with an _id field, you’ll have to ensure that the value of the field is unique because the _id has to be unique.

The _id field gets a unique index automatically too. That’s what adds the requirement for _id fields to be unique. And that means you can’t have the same _id for any two documents in a collection.

As the _id field is immutable, you cannot change it and that’s by design. This field is all about being the immovable reference to the document. Changing the _id, if allowed, could start off index rebuilding. That in turn could end up requiring the entire collection to be locked at some level while that happened. So, MongoDB avoids all that and declares the _id immutable. With it not changing, MongoDB can make assumptions about how to handle the index for _id more effectively as it is typically the most accessed field in the database. 

The biggest indication that you should not regard the _id field as part of your schema is the underscore at the start. Many developers and some languages use that as an indication that a variable is for internal/private use only and that’s a good way to look at _id.

How do I change the _id field then?

Well, ideally you don’t. MongoDB won’t let you:

db> db.listings.updateOne({}, { $set: { _id:ObjectId() }})

MongoServerError: Performing an update on the path '_id' would modify the immutable field '_id'

You may consider the option to delete the document and reinsert it with a new _id field, but you run a number of risks doing that. Your new _id may not be unique and the insert will fail. Or something bad happens to the connection and the insert fails. 

Whichever way, the original document will be no longer be there. Ah, you may think, what if I insert the new document then delete the old document. Well, the issue of the new _id not being unique still exists. Add to that, if there’s another unique index for the collection, the new record will be rejected for failing the uniqueness constraint of that index. 

Consider it a design problem. If you want to have a field which can be changed on demand and is indexed, then create a field and an index where you can do that. You can decide on whether you want that index to be unique or not, something you don’t get to select if you are loading the _id field with important meaningful values.

At the end of the day, there’s a lot of good reasons for leaving the _id field in its immutable isolation.


How helpful was this article?
This article was hideous
This article was bad
This article was ok
This article was good
This article was great
Thank you for your feedback!

About The Author

Dj Walker-Morgan

Dj has been around since Commodore had Pets and Apples grew everywhere. With a background in Unix and development, he's been around the technology business writing code or writing content ever since.

Related articles

  • The Quickest Way to Change a Field Type in MongoDB
  • Is there a quicker way to query _id values? #Studio3T_AMA
  • Exercise 3: Adding a computed ratio field based on the converted lookup field
  • Why do I see an error when I re-run an IntelliShell script? #Studio3T_AMA
  • Help! How can I find my lost query! #Studio3T_AMA

Tags

2022 academy aggregation AMA atlas Certification christmas community connections culture date tags events export features hackolade import intellishell In Use JSON knowledge base migration modelling mongodb mongoodb mongosh My 3T productivity query regex releases schema security SQL Studio 3T tasks time series tips updates webinar windows

Browse by MongoDB topic

  • Connecting to MongoDB
  • Database Administration & Security
  • Getting Started with MongoDB
  • Getting Started with Studio 3T
  • Import/Export
  • Job Automation & Scheduling
  • MongoDB Aggregation Framework
  • MongoDB/Studio 3T Workshops
  • Performance
  • Query & CRUD Operations
  • Reference
  • Schema
  • Studio 3T Licensing
  • Support and other resources
  • Working with MongoDB & SQL
  • Working with MongoDB Atlas

Studio 3T

MongoDB Enterprise Certified Technology PartnerSince 2014, 3T has been helping thousands of MongoDB developers and administrators with their everyday jobs by providing the finest MongoDB tools on the market. We guarantee the best compatibility with current and legacy releases of MongoDB, continue to deliver new features with every new software release, and provide high quality support.

Find us on FacebookFind us on TwitterFind us on YouTubeFind us on LinkedIn

Education

  • Free MongoDB Tutorials
  • Connect to MongoDB
  • Connect to MongoDB Atlas
  • Import Data to MongoDB
  • Export MongoDB Data
  • Build Aggregation Queries
  • Query MongoDB with SQL
  • Migrate from SQL to MongoDB

Resources

  • Feedback and Support
  • Sales Support
  • Knowledge Base
  • FAQ
  • Reports
  • White Papers
  • Testimonials
  • Discounts

Company

  • About Us
  • Blog
  • Careers
  • Legal
  • Press
  • Privacy Policy
  • EULA

© 2023 3T Software Labs Ltd. All rights reserved.

  • Privacy Policy
  • Cookie settings
  • Impressum

We value your privacy

With your consent, we and third-party providers use cookies and similar technologies on our website to analyse your use of our site for market research or advertising purposes ("analytics and marketing") and to provide you with additional functions (“functional”). This may result in the creation of pseudonymous usage profiles and the transfer of personal data to third countries, including the USA, which may have no adequate level of protection for the processing of personal data.

By clicking “Accept all”, you consent to the storage of cookies and the processing of personal data for these purposes, including any transfers to third countries. By clicking on “Decline all”, you do not give your consent and we will only store cookies that are necessary for our website. You can customize the cookies we store on your device or change your selection at any time - thus also revoking your consent with effect for the future - under “Manage Cookies”, or “Cookie Settings” at the bottom of the page. You can find further information in our Privacy Policy.
Accept all
Decline all
Manage cookies
✕

Privacy Preference Center

With your consent, we and third-party providers use cookies and similar technologies on our website to analyse your use of our site for market research or advertising purposes ("analytics and marketing") and to provide you with additional functions (“functional”). This may result in the creation of pseudonymous usage profiles and the transfer of personal data to third countries, including the USA, which may have no adequate level of protection for the processing of personal data. Please choose for which purposes you wish to give us your consent and store your preferences by clicking on “Accept selected”. You can find further information in our Privacy Policy.

Accept all cookies

Manage consent preferences

Essential cookies are strictly necessary to provide an online service such as our website or a service on our website which you have requested. The website or service will not work without them.

Performance cookies allow us to collect information such as number of visits and sources of traffic. This information is used in aggregate form to help us understand how our websites are being used, allowing us to improve both our website’s performance and your experience.

Google Analytics

Google Ads

Bing Ads

Facebook

LinkedIn

Quora

Hotjar

Reddit

Functional cookies collect information about your preferences and choices and make using the website a lot easier and more relevant. Without these cookies, some of the site functionality may not work as intended.

HubSpot

Social media cookies are cookies used to share user behaviour information with a third-party social media platform. They may consequently effect how social media sites present you with information in the future.

Accept selected