
- 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
MongoDB transaction & indexes for duplicate values
Use ensureIndex() and set unique:1 inside the array. Let us create a collection with documents −
> db.demo298.save({Name: 'Chris', Marks: [46, 79] }); WriteResult({ "nInserted" : 1 }) > db.demo298.save({Name: 'David', Marks: [67, 88] }); WriteResult({ "nInserted" : 1 }) > db.demo298.ensureIndex({ Marks: 1 }, {unique: 1}); { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo298.save({Name: 'Mike', Marks: [88,98] }); WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "E11000 duplicate key error collection: test.demo298 index: Marks_1 dup key: { : 88.0 }" } })
Display all documents from a collection with the help of find() method −
> db.demo298.find();
This will produce the following output −
{ "_id" : ObjectId("5e4d56e55d93261e4bc9ea48"), "Name" : "Chris", "Marks" : [ 46, 79 ] } { "_id" : ObjectId("5e4d56f55d93261e4bc9ea49"), "Name" : "David", "Marks" : [ 67, 88 ] }
- Related Articles
- Find maximum score for duplicate Name values in MongoDB?
- MongoDB aggregation group and remove duplicate array values?
- Rebuilding indexes in MongoDB?
- How to search an array for values present in another array and output the indexes of values found into a new array in MongoDB?
- Transaction lock in MongoDB?
- Get the duplicate values of a field in MongoDB?
- How to remove duplicate values inside a list in MongoDB?
- Sum the score of duplicate column values in MongoDB documents?
- Building Multiple Indexes at once in MongoDB?
- MongoDB indexes not working when executing $elemMatch?
- How can I clone/duplicate the table along with its data, trigger and indexes?
- MongoDB aggregate to get the count of field values of corresponding duplicate names?
- Find duplicate records in MongoDB?
- Avoid duplicate entries in MongoDB?
- Differentiate between cash transaction and credit transaction

Advertisements