
- 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
Perform MongoDB full text search
For full text search in MongoDB, use $text. The $text performs a text search on the content of the fields. Let us create a collection with documents −
> db.demo654.createIndex({Name:"text"}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo654.insertOne({"Name":"John Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea04e924deddd72997713c8") } > db.demo654.insertOne({"Name":"Chris Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea04e9d4deddd72997713c9") } > db.demo654.insertOne({"Name":"John Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea04ea54deddd72997713ca") } > db.demo654.insertOne({"Name":"David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea04eb24deddd72997713cb") }
Display all documents from a collection with the help of find() method −
> db.demo654.find();
This will produce the following output −
{ "_id" : ObjectId("5ea04e924deddd72997713c8"), "Name" : "John Doe" } { "_id" : ObjectId("5ea04e9d4deddd72997713c9"), "Name" : "Chris Brown" } { "_id" : ObjectId("5ea04ea54deddd72997713ca"), "Name" : "John Smith" } { "_id" : ObjectId("5ea04eb24deddd72997713cb"), "Name" : "David Miller" }
Following is the query to perform full text search in MongoDB −
> db.demo654.find({ $text: { $search: "John" } } );
This will produce the following output −
{ "_id" : ObjectId("5ea04ea54deddd72997713ca"), "Name" : "John Smith" } { "_id" : ObjectId("5ea04e924deddd72997713c8"), "Name" : "John Doe" }
- Related Articles
- Implement Text search in MongoDB
- Perform nested document value search in MongoDB?
- Set search feature in MySQL for full text searching
- Text search in MongoDB with Regular Expression
- Using a regex with text search in MongoDB
- Create an index for text search in MongoDB
- 8085 Program to perform linear search
- Perform MySQL search between two dates
- Common Linux Text Search
- Perform aggregation sort in MongoDB?
- Perform simple validation in MongoDB?
- Perform Bulk insert in MongoDB?
- Perform Binary Search in Java using Collections.binarySearch
- C++ Program to Perform Uniform Binary Search
- Perform Binary Search on ArrayList with Java Collections

Advertisements