
- 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
Check if a field contains a string in MongoDB?
You can use $regex operator to check if a field contains a string in MongoDB. The syntax is as follows −
db.yourCollectionName.findOne({"yourFieldName":{$regex:".*yourValue.*"}});
To understand the above concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.checkFieldContainsStringDemo.insertOne({"Id":1,"Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77d762fc4e719b197a12ed") } > db.checkFieldContainsStringDemo.insertOne({"Id":2,"Name":"Johnson"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77d76bfc4e719b197a12ee") } > db.checkFieldContainsStringDemo.insertOne({"Id":3,"Name":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77d774fc4e719b197a12ef") } > db.checkFieldContainsStringDemo.insertOne({"Id":4,"Name":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77d77cfc4e719b197a12f0") } > db.checkFieldContainsStringDemo.insertOne({"Id":5,"Name":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77d784fc4e719b197a12f1") } > db.checkFieldContainsStringDemo.insertOne({"Id":6,"Name":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77d78cfc4e719b197a12f2") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.checkFieldContainsStringDemo.find();
The following is the output −
{ "_id" : ObjectId("5c77d762fc4e719b197a12ed"), "Id" : 1, "Name" : "John" } { "_id" : ObjectId("5c77d76bfc4e719b197a12ee"), "Id" : 2, "Name" : "Johnson" } { "_id" : ObjectId("5c77d774fc4e719b197a12ef"), "Id" : 3, "Name" : "Carol" } { "_id" : ObjectId("5c77d77cfc4e719b197a12f0"), "Id" : 4, "Name" : "Mike" } { "_id" : ObjectId("5c77d784fc4e719b197a12f1"), "Id" : 5, "Name" : "Sam" } { "_id" : ObjectId("5c77d78cfc4e719b197a12f2"), "Id" : 6, "Name" : "Larry" }
Here is the query to check the field contains a particular string in MongoDB. Here the string we are searching in a field is “Johnson” −
> db.checkFieldContainsStringDemo.findOne({"Name":{$regex:".*Johnson.*"}});
The following is the output −
{ "_id" : ObjectId("5c77d76bfc4e719b197a12ee"), "Id" : 2, "Name" : "Johnson" }
- Related Articles
- Check if a string contains a sub-string in C++
- Check if a String Contains a Substring in Linux
- Check if a string contains numbers in MySQL?
- How do I check whether a field contains null value in MongoDB?
- Check if a string contains a number using Java.
- Check if value exists for a field in a MongoDB document?
- How to check if a string contains a specific sub string?
- How to check if field is a number in MongoDB?
- How to check if a field in MongoDB is [] or {}?
- How to check if a string contains a substring in Golang?
- MySQL query to check if a string contains a word?
- Java Program to Check if a string contains a substring
- Golang program to check if a string contains a substring
- Check if string contains another string in Swift
- Method to check if a String contains a sub string ignoring case in Java

Advertisements