
- 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
Changing the primary key on a MongoDB collection?
To change the primary key, you need to first delete it. Use forEach() along with delete to remove and then get a new primary key. Let us create a collection with documents −
> db.demo41.insertOne({"StudentName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5e25ce4acfb11e5c34d898e3") }
Display all documents from a collection with the help of find() method −
> db.demo41.find();
This will produce the following output −
{ "_id" : ObjectId("5e25ce4acfb11e5c34d898e3"), "StudentName" : "Carol" }
Here is the query to change the primary key on a MongoDB collection −
> var next = db.demo41.find() > > next.forEach(function(s) { ... var prevId=s._id; ... delete s._id; ... db.demo41.insert(s); ... db.demo41.remove(prevId); ... });
Let us check the primary key once again −
> db.demo41.find();
This will produce the following output displaying a new primary key −
{ "_id" : ObjectId("5e25cee5cfb11e5c34d898e4"), "StudentName" : "Carol" }
- Related Articles
- How to remove primary key from MongoDB?
- How to get the child of a MongoDB collection by the key?
- Primary key Vs Unique key
- Find all duplicate documents in a MongoDB collection by a key field?
- How to read a specific key-value pair from a MongoDB collection?
- How can I define a column of a MySQL table PRIMARY KEY without using the PRIMARY KEY keyword?
- Create Primary Key on an existing table in PostgreSQL?
- Difference between Primary Key and Candidate key
- Difference between Primary Key and Unique key
- How to sum the value of a key across all documents in a MongoDB collection?
- Set max on MongoDB capped collection?
- Remove Primary Key in MySQL?
- Reset Primary Key in MySQL
- How can we set PRIMARY KEY on multiple columns of a MySQL table?
- Difference between Primary key and Foreign key in Database

Advertisements