
- 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 make a unique field in MongoDB?
To make a unique field in MongoDB, use unique − true. Let us create a collection with documents −
> db.demo82.createIndex({"EmployeeName":1},{unique:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo82.insertOne({"EmployeeName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bfb1b71bf0181ecc422a0") } > db.demo82.insertOne({"EmployeeName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bfb1f71bf0181ecc422a1") } > db.demo82.insertOne({"EmployeeName":"Chris"}); 2020-01-25T13:54:00.410+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: test.demo82 index: EmployeeName_1 dup key: { : "Chris" } : WriteError({ "index" : 0, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: test.demo82 index: EmployeeName_1 dup key: { : \"Chris\" }", "op" : { "_id" : ObjectId("5e2bfb2071bf0181ecc422a2"), "EmployeeName" : "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.demo82.find();
This will produce the following output. Since, we use unique − true above, therefore the duplicate values won’t get insert −
{ "_id" : ObjectId("5e2bfb1b71bf0181ecc422a0"), "EmployeeName" : "Chris" } { "_id" : ObjectId("5e2bfb1f71bf0181ecc422a1"), "EmployeeName" : "David" }
- Related Articles
- How to make an existing field Unique in MySQL?
- Add field that is unique index to collection in MongoDB?
- Indexing large text field to make query faster in MongoDB
- How to increment a field in MongoDB?
- How to make a Country field in Django?
- Change a unique index to a sparse unique index in MongoDB?
- How to insert a boolean field in MongoDB?
- How to select a single field in MongoDB?
- How to make a pair of columns unique in MySQL?
- How to keep two “columns” in MongoDB unique?
- How to determine whether a field exists in MongoDB?
- How to check empty field in a MongoDB collection?
- How to compare field values in MongoDB?
- How to improve querying field in MongoDB?
- How to update _id field in MongoDB?

Advertisements