
- 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
Find which MongoDB document contains a specific string?
To find which document contains a specific string, use $regex along with find(). Let us create a collection with documents −
> db.demo597.insertOne({"Name":"John Doe"});{ "acknowledged" : true, "insertedId" : ObjectId("5e947ae3f5f1e70e134e2690") } > db.demo597.insertOne({"Name":"John Smith"});{ "acknowledged" : true, "insertedId" : ObjectId("5e947ae8f5f1e70e134e2691") } > db.demo597.insertOne({"Name":"Chris Brown"});{ "acknowledged" : true, "insertedId" : ObjectId("5e947aeff5f1e70e134e2692") } > db.demo597.insertOne({"Name":"Adam Smith"});{ "acknowledged" : true, "insertedId" : ObjectId("5e947afff5f1e70e134e2693") }
Display all documents from a collection with the help of find() method −
> db.demo597.find();
This will produce the following output −
{ "_id" : ObjectId("5e947ae3f5f1e70e134e2690"), "Name" : "John Doe" } { "_id" : ObjectId("5e947ae8f5f1e70e134e2691"), "Name" : "John Smith" } { "_id" : ObjectId("5e947aeff5f1e70e134e2692"), "Name" : "Chris Brown" } { "_id" : ObjectId("5e947afff5f1e70e134e2693"), "Name" : "Adam Smith" }
Following is the query to find which MongoDB document contains a specific string −
> db.demo597.find({Name:{$regex:/smith/i}} );
This will produce the following output −
{ "_id" : ObjectId("5e947ae8f5f1e70e134e2691"), "Name" : "John Smith" } { "_id" : ObjectId("5e947afff5f1e70e134e2693"), "Name" : "Adam Smith" }
- Related Articles
- Find document with array that contains a specific value in MongoDB
- Extract a MongoDB document with a specific string
- Find MongoDB documents that contains specific field?
- Return specific MongoDB embedded document
- MongoDB query to remove a specific document
- Filter specific values from a MongoDB document
- Find documents that contains specific field in MongoDB?
- Find the document by field name with a specific value in MongoDB?
- MongoDB query to find documents whose array contains a string that is a substring of a specific word
- Update only a specific value in a MongoDB document
- Find a strict document that contains only a specific field with a fixed length?\n
- Fetch a specific document in MongoDB with array elements
- Find MongoDB document with array containing the maximum occurrence of a specific value
- How to find MongoDB documents with a specific string?
- MongoDB query to update a specific document from a collection

Advertisements