
- 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
Indexing large text field to make query faster in MongoDB
To index large text field, use ensureIndex() along with $regex for text search. Let us create a collection with documents −
> db.demo46.ensureIndex({"Name":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo46.insertOne({"Name":"John Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e267004cfb11e5c34d898ed") } > db.demo46.insertOne({"Name":"John Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e267009cfb11e5c34d898ee") } > db.demo46.insertOne({"Name":"Chris Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5e267011cfb11e5c34d898ef") }
Display all documents from a collection with the help of find() method −
> db.demo46.find();
This will produce the following output −
{ "_id" : ObjectId("5e267004cfb11e5c34d898ed"), "Name" : "John Smith" } { "_id" : ObjectId("5e267009cfb11e5c34d898ee"), "Name" : "John Doe" } { "_id" : ObjectId("5e267011cfb11e5c34d898ef"), "Name" : "Chris Brown" }
Following is the query to index large text field to make query faster −
> db.demo46.find({ Name: { $regex:/^John/}});
This will produce the following output −
{ "_id" : ObjectId("5e267009cfb11e5c34d898ee"), "Name" : "John Doe" } { "_id" : ObjectId("5e267004cfb11e5c34d898ed"), "Name" : "John Smith" }
- Related Articles
- How to query on list field in MongoDB?
- MongoDB query by sub-field?
- How to make a unique field in MongoDB?
- How to trim spaces from field in MongoDB query?
- MongoDB query for a single field
- MongoDB query to update array with another field?
- How can I enhance my select query to make it faster in MySQL?
- How to make a case-insensitive query in MongoDB?
- MongoDB query to change simple field into an object?
- How to make Laravel (Blade) text field read-only?
- MongoDB query (aggregation framework) to match a specific field value
- MongoDB query to match documents that contain an array field
- MongoDB query to limit the returning values of a field?
- MongoDB Query for boolean field as “not true”
- Field selection within MongoDB query using dot notation?

Advertisements