
- 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 restrict inserting an item with the same name in MongoDB?
For this, use ensureIndex() and set unique:true. Let us create a collection with documents. Here, when we try to inert duplicate item, then a duplicate key error occurs −
> db.demo316.ensureIndex({"SubjectName":1},{unique:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo316.insertOne({"SubjectName":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50e378f8647eb59e56205d") } > db.demo316.insertOne({"SubjectName":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50e37df8647eb59e56205e") } > db.demo316.insertOne({"SubjectName":"MongoDB"}); 2020-02-22T13:47:05.186+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: test.demo316 index: SubjectName_1 dup key: { : "MongoDB" } : WriteError({ "index" : 0, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: test.demo316 index: SubjectName_1 dup key: { : \"MongoDB\" }", "op" : { "_id" : ObjectId("5e50e381f8647eb59e56205f"), "SubjectName" : "MongoDB" } }) 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.demo316.find();
This will produce the following output −
{ "_id" : ObjectId("5e50e378f8647eb59e56205d"), "SubjectName" : "MySQL" } { "_id" : ObjectId("5e50e37df8647eb59e56205e"), "SubjectName" : "MongoDB" }
- Related Articles
- How to add new item in nested array with MongoDB?
- How to select maximum item in each group with MongoDB?
- How to insert an item to an array that is inside an object in MongoDB?
- How can I delete an item from an Object in MongoDB?
- Search for documents matching first item in an array with MongoDB?
- Inserting the current datetime in MongoDB?
- Python - Inserting item in sorted list maintaining order
- MongoDB query to set a sub item in an array?
- Finding a specific item in subdocument with MongoDB?
- Getting only the first item for an array property in MongoDB?
- How to update a MongoDB document for adding a new item to an array?
- Escaping quotes while inserting records in MongoDB?
- Inserting Date() in MongoDB through Mongo shell?
- MongoDB query with fields in the same document?
- Restrict returned data in MongoDB to display only specific values from documents

Advertisements