
- 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 duplicates from MongoDB Collection?
For this, set “unique:true” i.e. the unique constraint and avoid inserting duplicates as in the below syntax −
db.yourCollectionName.ensureIndex({yourFieldName: 1}, {unique: true, dropDups: true})
To understand the above syntax, let us create a collection with documents. Here, duplicate insertion won’t be allowed −
> db.demo604.ensureIndex({FirstName: 1}, {unique: true, dropDups: true});{ "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo604.insertOne({FirstName:"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e960887ed011c280a0905d8") } > db.demo604.insertOne({FirstName:"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e96088aed011c280a0905d9") } > db.demo604.insertOne({FirstName:"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e96088ded011c280a0905da") } > db.demo604.insertOne({FirstName:"Chris"}); 2020-04-15T00:31:35.978+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: test.demo604 index: FirstName_1 dup key: { : "Chris" } : WriteError({ "index" : 0, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: test.demo604 index: FirstName_1 dup key: { : \"Chris\" }", "op" : { "_id" : ObjectId("5e96088fed011c280a0905db"), "FirstName" : "Chris" } }) WriteError@src/mongo/shell/bulk_api.js:461:48 Bulk/mergeBatchResults@src/mongo/shell/bulk_api.js:841:49 Bulk/executeBatch@src/mongo/shell/bulk_api.js:906:13 Bulk/this.execute@src/mongo/shell/bulk_api.js:1150:21 DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:252:9 @(shell):1:1
Display all documents from a collection with the help of find() method −
> db.demo604.find();
This will produce the following output −
{ "_id" : ObjectId("5e960887ed011c280a0905d8"), "FirstName" : "Chris" } { "_id" : ObjectId("5e96088aed011c280a0905d9"), "FirstName" : "Bob" } { "_id" : ObjectId("5e96088ded011c280a0905da"), "FirstName" : "David" }
- Related Articles
- MongoDB query to remove entire array from collection?
- MongoDB query to remove entire data from a collection
- How to remove all documents from a collection except a single document in MongoDB?
- How to remove duplicates from an ArrayList in Java?
- Python - Ways to remove duplicates from list
- How to remove all elements from a Collection in another Collection
- How to drop a numeric collection from MongoDB?
- How to get array from a MongoDB collection?
- How to get unique values from MongoDB collection?
- Golang Program To Remove Duplicates From An Array
- How to remove duplicates from a sorted linked list in android?
- How to find and remove duplicates from a table in Oracle?
- Remove document whose value is matched with $eq from a MongoDB collection?
- How to remove primary key from MongoDB?
- How to retrieve all nested fields from MongoDB collection?

Advertisements