
- 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 efficiently run complex queries on MongoDB unindexed fields?
Create an index to efficiently run complex queries. Let us first create a collection with documents −
> db.demo400.insertOne({SubjectName:"Java Spring"}); { "acknowledged" : true, "insertedId" : ObjectId("5e610720fac4d418a0178572") } > db.demo400.insertOne({SubjectName:"Spring Hibernate"}); { "acknowledged" : true, "insertedId" : ObjectId("5e61072dfac4d418a0178573") } > db.demo400.insertOne({SubjectName:"Java Hibernate"}); { "acknowledged" : true, "insertedId" : ObjectId("5e610736fac4d418a0178574") } > db.demo400.createIndex({SubjectName:"text"}); { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
Display all documents from a collection with the help of find() method −
> db.demo400.find();
This will produce the following output −
{ "_id" : ObjectId("5e610720fac4d418a0178572"), "SubjectName" : "Java Spring" } { "_id" : ObjectId("5e61072dfac4d418a0178573"), "SubjectName" : "Spring Hibernate" } { "_id" : ObjectId("5e610736fac4d418a0178574"), "SubjectName" : "Java Hibernate" }
Following is the query to efficiently perform complex queries on unindexed fields −
> db.demo400.find({ $text: { $search: "Spring" } } )
This will produce the following output −
{ "_id" : ObjectId("5e61072dfac4d418a0178573"), "SubjectName" : "Spring Hibernate" } { "_id" : ObjectId("5e610720fac4d418a0178572"), "SubjectName" : "Java Spring" }
- Related Articles
- How do I make case-insensitive queries on MongoDB?
- How to efficiently perform “distinct” with multiple keys in MongoDB?
- Performing distinct on multiple fields in MongoDB?
- MongoDB query condition on comparing 2 fields?
- How to remove key fields in MongoDB?
- MongoDB query for exact match on multiple document fields
- How to run MongoDB shell using mongos command?\n
- How to find MongoDB documents in a collection with a filter on multiple combined fields?
- How to improve MongoDB queries with multikey index in array?
- How to retrieve all nested fields from MongoDB collection?
- How to run Gunicorn on Docker?
- MongoDB query to sum specific fields
- MongoDB query to update selected fields
- How do I access subdocuments in MongoDB queries?
- Concatenate fields in MongoDB?

Advertisements