
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How to remove primary key from MongoDB?
To remove primary key in MongoDB, set _id value to 0 i.e. set the field you want to exclude as 0 in find(). Let us create a collection with documents −
> db.demo471.insertOne({"ClientId":101,"ClientName":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e805711b0f3fa88e2279077") } > db.demo471.insertOne({"ClientId":102,"ClientName":"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e80571db0f3fa88e2279078") } > db.demo471.insertOne({"ClientId":103,"ClientName":"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e805724b0f3fa88e2279079") }
Display all documents from a collection with the help of find() method −
> db.demo471.find();
This will produce the following output −
{ "_id" : ObjectId("5e805711b0f3fa88e2279077"), "ClientId" : 101, "ClientName" : "Chris" } { "_id" : ObjectId("5e80571db0f3fa88e2279078"), "ClientId" : 102, "ClientName" : "Bob" } { "_id" : ObjectId("5e805724b0f3fa88e2279079"), "ClientId" : 103, "ClientName" : "David" }
Following is the query to remove primary key in MongoDB −
> db.demo471.find({},{_id:0,ClientId:0});;
This will produce the following output −
{ "ClientName" : "Chris" } { "ClientName" : "Bob" } { "ClientName" : "David" }
- Related Articles
- Remove Primary Key in MySQL?
- Can we remove a primary key from MySQL table?
- How to remove key fields in MongoDB?
- MySQL ALTER column to remove primary key and auto_increment?
- How to make a primary key start from 1000?
- How can we remove PRIMARY KEY constraint from a column of an existing MySQL table?
- Changing the primary key on a MongoDB collection?
- Primary key Vs Unique key
- How to remove duplicates from MongoDB Collection?
- How to remove a key from a python dictionary?
- How to get primary key value (auto-generated keys) from inserted queries using JDBC?
- How to make MySQL table primary key auto increment?
- Difference between Primary Key and Candidate key
- Difference between Primary Key and Unique key
- How to remove object from array in MongoDB?

Advertisements