Remove by _id in MongoDB console
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Removing Documents by _id in MongoDB
MongoDB, a popular NoSQL database, is designed for flexibility and scalability, allowing the storage of data in the JSON-like BSON format. One of the core features of MongoDB is the ability to efficiently perform CRUD (Create, Read, Update, Delete) operations on collections of documents. Among these operations, deletion of documents by their unique identifier _id is a common requirement. This article will delve into the technical details of removing documents by _id in the MongoDB console.
Understanding the _id Field
In MongoDB, every document has a unique _id field, which serves as a primary key. When inserting documents, MongoDB automatically generates a unique ObjectId for this field if it is not provided by the user. The _id field guarantees uniqueness within a collection, thus making it a reliable identifier for operations such as updates or deletes.
Using the remove() Method
To remove a document by its _id, you can use the remove() method in the MongoDB shell, a part of their Console. This method provides a straightforward interface to specify criteria for document deletion. The command generally follows this syntax:
Parameters:
<query>: A document that specifies the criteria for deletion. To remove by_id, you provide a criterion document like{"_id": ObjectId("yourObjectId")}.<justOne>(optional): A boolean determining whether to remove only one document (defaults tofalse).
Example: Removing a Document by _id
- Connecting to Your MongoDB DatabaseBefore performing any operations, ensure you have connected to your MongoDB instance. This typically involves opening the MongoDB shell and selecting the database you're working with.
- Identifying the Document to RemoveFirst, fetch the document you want to delete to verify its
_id. For instance:
Let's assume the output returns:
- Executing the
remove()MethodUse the fetched_idto remove the document:
This command removes the document associated with that specific _id.
Notes on Deprecation
It's worth mentioning that starting from MongoDB 3.2, the remove() method was deprecated in favor of the deleteOne() and deleteMany() methods, which offer more clarity and options for bulk operations.
deleteOne(): Removes a single document based on a filter.
deleteMany(): Removes multiple documents matching a filter, though for an_idbased query, it behaves the same asdeleteOne().
Additional Considerations
- Atomicity: Removing a document by
_idis atomic, meaning that the operation will either fully succeed or fail, with no intermediate states. - Indexes: MongoDB automatically indexes
_id, allowing for fast lookups when removing documents.
Summary Table
| Key Aspect | Description |
_id Field | Unique identifier for each document |
remove() Method | Syntax: db.collection.remove(query) |
| Deprecation Notice | Use deleteOne() instead from version 3.2 |
| Atomicity | Operations on _id are atomic |
| Automatic Index | _id is automatically indexed for performance |
Conclusion
Removing documents by _id in MongoDB is a critical operation, often required for maintaining data integrity and management within a collection. Understanding how to effectively use the remove() method and its modern alternatives like deleteOne() enhances both performance and clarity in database manipulation tasks. As MongoDB evolves, staying updated with its methods and best practices ensures that your database operations remain efficient and error-free.
Related reading

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.