
- 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
Add field that is unique index to collection in MongoDB?
For unique index, set unique − true while creating an index. Let us create a collection with documents −
> db.demo658.createIndex({FirstName:1},{unique:true,sparse:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > > db.demo658.insertOne({"FirstName":"John","LastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea067144deddd72997713d9") } > db.demo658.insertOne({"FirstName":"Adam","LastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea0671c4deddd72997713da") } > db.demo658.insertOne({"FirstName":"John","LastName":"Doe"}); 2020-04-22T21:17:46.072+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: onlinecustomertracker.demo658 index: FirstName_1 dup key: { : "John" } : WriteError({ "index" : 0, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: onlinecustomertracker.demo658 index: FirstName_1 dup key: { : \"John\" }", "op" : { "_id" : ObjectId("5ea067224deddd72997713db"), "FirstName" : "John", "LastName" : "Doe" } }) 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.demo658.find();
This will produce the following output −
{ "_id" : ObjectId("5ea067144deddd72997713d9"), "FirstName" : "John", "LastName" : "Smith" } { "_id" : ObjectId("5ea0671c4deddd72997713da"), "FirstName" : "Adam", "LastName" : "Smith" }
- Related Articles
- Add new field to every document in a MongoDB collection?
- Change a unique index to a sparse unique index in MongoDB?
- Set unique index in MongoDB
- How to add a new field to all the documents in a MongoDB collection
- Create index in a MongoDB collection?
- How to make a unique field in MongoDB?
- How to get unique values from MongoDB collection?
- How to check empty field in a MongoDB collection?
- How to add a column in MongoDB collection?
- Add MD5 hash value to MongoDB collection?
- Project specific array field in a MongoDB collection?
- Export specified field of a collection in mongodb / mongodump to file?
- Finding documents in MongoDB collection where a field is equal to given integer value?
- How to update a single field in a capped collection in MongoDB?
- Script to add a single value to array in MongoDB collection?

Advertisements