
- 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
Implement Text search in MongoDB
For text search, you need to use $text along with $search. Let us create a collection with documents −
> db.demo156.createIndex({"StudentName":"text"}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo156.insertOne({"StudentName":"Chris Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3547e8fdf09dd6d08539e6") } > db.demo156.insertOne({"StudentName":"John Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3547f2fdf09dd6d08539e7") } > db.demo156.insertOne({"StudentName":"John Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3547f7fdf09dd6d08539e8") }
Display all documents from a collection with the help of find() method −
> db.demo156.find();
This will produce the following output −
{ "_id" : ObjectId("5e3547e8fdf09dd6d08539e6"), "StudentName" : "Chris Brown" } { "_id" : ObjectId("5e3547f2fdf09dd6d08539e7"), "StudentName" : "John Doe" } { "_id" : ObjectId("5e3547f7fdf09dd6d08539e8"), "StudentName" : "John Smith" }
Following is the query to implement text search in MongoDB −
> db.demo156.find({ $text: { $search: "John" } } )
This will produce the following output −
{ "_id" : ObjectId("5e3547f7fdf09dd6d08539e8"), "StudentName" : "John Smith" } { "_id" : ObjectId("5e3547f2fdf09dd6d08539e7"), "StudentName" : "John Doe" }
- Related Articles
- Perform MongoDB full text search
- Text search in MongoDB with Regular Expression
- Using a regex with text search in MongoDB
- Create an index for text search in MongoDB
- C++ Program to Implement the String Search Algorithm for Short Text Sizes
- Implement multiple conditions in MongoDB?
- Implement array match in MongoDB?
- Common Linux Text Search
- Search for multiple documents in MongoDB?
- Search by specific field in MongoDB
- Java program to implement binary search
- Java program to implement linear search
- How to implement a Keyword Search in MySQL?
- Adding default search text to search box in HTML with JavaScript?
- MongoDB Query to implement $in in array

Advertisements