
- 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
Set unique index in MongoDB
To set unique index in MongoDB, use unique:true. Let us create a collection with documents −
> db.demo257.ensureIndex({Name:1},{unique:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo257.insertOne({Name:"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47a5e51627c0c63e7dba91") } > db.demo257.insertOne({Name:"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47a5e81627c0c63e7dba92") } > db.demo257.insertOne({Name:"Chris"}); 2020-02-15T13:33:54.064+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: test.demo257 index: Name_1 dup key: { : "Chris" } : WriteError({ "index" : 0, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: test.demo257 index: Name_1 dup key: { : \"Chris\" }", "op" : { "_id" : ObjectId("5e47a5ea1627c0c63e7dba93"), "Name" : "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.demo257.find();
This will produce the following output −
{ "_id" : ObjectId("5e47a5e51627c0c63e7dba91"), "Name" : "Chris" } { "_id" : ObjectId("5e47a5e81627c0c63e7dba92"), "Name" : "Bob" }
- Related Articles
- Change a unique index to a sparse unique index in MongoDB?
- Add field that is unique index to collection in MongoDB?
- Set MongoDB compound index with a fixed value field
- How to create unique index in Android sqlite?
- Python Pandas - Return unique values in the index
- Combining unique items from arrays in MongoDB?
- Create index in a MongoDB collection?
- How to make a unique field in MongoDB?
- How to keep two “columns” in MongoDB unique?
- How does MongoDB index arrays?
- Build index in the background with MongoDB
- MongoDB query to gather unique array item?
- Set regex in MongoDB $in?
- Python Pandas - Check if the index has unique values
- Python Pandas - Return number of unique elements in the Index object

Advertisements