
- 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 create an index with MongoDB?
To create an index in MongoDB, use the ensureIndex() method. Let us first create a collection using following query
> db.createCollection(&qu/ot;creatingUniqueIndexDemo"); { "ok" : 1 }
Following is the query to create an index on the above collection:
> db.creatingUniqueIndexDemo.ensureIndex({"UserCountryName":1},{unique:true}); { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
Following is the query to insert some documents in the above collection
>db.creatingUniqueIndexDemo.insertOne({"UserName":"John","UserAge":21,"UserCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9947bd330fd0aa0d2fe4d8") } >db.creatingUniqueIndexDemo.insertOne({"UserName":"Mike","UserAge":23,"UserCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9947c9330fd0aa0d2fe4d9") } >db.creatingUniqueIndexDemo.insertOne({"UserName":"Robert","UserAge":26,"UserCountryName":"US"}); 2019-03-26T02:57:52.670+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: test.creatingUniqueIndexDemo index: UserCountryName_1 dup key: { : "US" } : WriteError({ "index" : 0, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: test.creatingUniqueIndexDemo index: . UserCountryName_1 dup key: { : \"US\" }", "op" : { "_id" : ObjectId("5c9947d8330fd0aa0d2fe4da"), "UserName" : "Robert", "UserAge" : 26, "UserCountryName" : "US" } }) 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 > db.creatingUniqueIndexDemo.insertOne({"UserName":"Robert","UserAge":26,"UserCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9947e3330fd0aa0d2fe4db") }
Following is the query to display all documents from a collection with the help of find() method
> db.creatingUniqueIndexDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9947bd330fd0aa0d2fe4d8"), "UserName" : "John", "UserAge" : 21, "UserCountryName" : "US" } { "_id" : ObjectId("5c9947c9330fd0aa0d2fe4d9"), "UserName" : "Mike", "UserAge" : 23, "UserCountryName" : "UK" } { "_id" : ObjectId("5c9947e3330fd0aa0d2fe4db"), "UserName" : "Robert", "UserAge" : 26, "UserCountryName" : "AUS" }
- Related Articles
- How to create an index in MongoDB using Java?
- How to create a nested index in MongoDB?
- Create an index for text search in MongoDB
- Create index in a MongoDB collection?
- How to drop an index in MongoDB using Java?
- Python Pandas - Create an Index with values cast to dtypes
- How to improve MongoDB queries with multikey index in array?
- How to remove an array element by its index in MongoDB?
- How to force MongoDB to use the BasicCursor instead of an index?\n
- How to index and sort with pagination using custom field in MongoDB?
- How does MongoDB index arrays?
- Build index in the background with MongoDB
- Creating an index on a nested MongoDB field?
- How to get the index of an array element in older versions on MongoDB?
- Create array with MongoDB query?

Advertisements