
- 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 do I index “or” in MongoDB for indexing multiple fields?
To index multiple fields, use ensureIndex() for a combination. With ensureIndex(), we can create an index and even pass multiple fields. Let us create a collection with documents −
> db.demo53.ensureIndex({"StudentFirstName":1,"StudentAge":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo53.ensureIndex({"StudentFirstName":1,"StudentCountryName":1}); { "createdCollectionAutomatically" : false, "numIndexesBefore" : 2, "numIndexesAfter" : 3, "ok" : 1 } >db.demo53.insertOne({"StudentFirstName":"Chris","StudentAge":21,"StudentCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e271431cfb11e5c34d89911") } >db.demo53.insertOne({"StudentFirstName":"David","StudentAge":23,"StudentCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5e27143ccfb11e5c34d89912") } >db.demo53.insertOne({"StudentFirstName":"Mike","StudentAge":24,"StudentCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5e27144bcfb11e5c34d89913") }
Display all documents from a collection with the help of find() method −
> db.demo53.find();
This will produce the following output −
{ "_id" : ObjectId("5e271431cfb11e5c34d89911"), "StudentFirstName" : "Chris", "StudentAge" : 21, "StudentCountryName" : "US" } { "_id" : ObjectId("5e27143ccfb11e5c34d89912"), "StudentFirstName" : "David", "StudentAge" : 23, "StudentCountryName" : "UK" } { "_id" : ObjectId("5e27144bcfb11e5c34d89913"), "StudentFirstName" : "Mike", "StudentAge" : 24, "StudentCountryName" : "AUS" }
- Related Articles
- Prevent duplicates of multiple fields with index in MongoDB
- Search multiple fields for multiple values in MongoDB?
- Array index or indexing inner items in MongoDB to fetch values
- MongoDB query for exact match on multiple document fields
- How do I work with array fields in MongoDB to match all?
- How do I search according to fields in inner classes using MongoDB db.coll.find()?
- Performing distinct on multiple fields in MongoDB?
- How do I use MongoDB to count only collections that match two fields?
- Count by multiple fields with MongoDB aggregation
- How do I plot multiple X or Y axes in Matplotlib?
- MongoDB - how can I access fields in a document?
- MongoDB query to check the existence of multiple fields
- MongoDB query for fields in embedded document?
- How do I sort natural in MongoDB?
- MongoDB $regex operator i or I for case insensitive search

Advertisements