
- 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
Query MongoDB with “like” implementation on name and email field beginning with a specific letter?
For “like” implementation in MongoDB, use / / and set that specific letter in between. For example −
/J/
Let us create a collection with documents −
> db.demo554.insertOne({"UserName":"John","UserMailId":"John@gmail.com"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f1cfed1d72c4545cb8679") } > db.demo554.insertOne({"UserName":"Chris","UserMailId":"Chris@gmail.com"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f1d0cd1d72c4545cb867a") } > db.demo554.insertOne({"UserName":"Jace","UserMailId":"Jace@gmail.com"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f1d1cd1d72c4545cb867b") }
Display all documents from a collection with the help of find() method −
> db.demo554.find();
This will produce the following output −
{ "_id" : ObjectId("5e8f1cfed1d72c4545cb8679"), "UserName" : "John", "UserMailId" : "John@gmail.com" } { "_id" : ObjectId("5e8f1d0cd1d72c4545cb867a"), "UserName" : "Chris", "UserMailId" : "Chris@gmail.com" } { "_id" : ObjectId("5e8f1d1cd1d72c4545cb867b"), "UserName" : "Jace", "UserMailId" : "Jace@gmail.com" }
Following is the query to implementation of “like” −
> db.demo554.find({ ... "$or": [ ... { "UserName": /J/ }, ... ... { "UserMailId": /J/ } ... ] ... } ... );
This will produce the following output −
{ "_id" : ObjectId("5e8f1cfed1d72c4545cb8679"), "UserName" : "John", "UserMailId" : "John@gmail.com" } { "_id" : ObjectId("5e8f1d1cd1d72c4545cb867b"), "UserName" : "Jace", "UserMailId" : "Jace@gmail.com" }
- Related Articles
- MongoDB query to search for string like “@email” in the field values
- MongoDB query to get record beginning with specific element from an array?
- Find the document by field name with a specific value in MongoDB?
- How to query MongoDB with “like”?
- MongoDB query to display documents with a specific name irrespective of case
- How to filter a query on specific date format with MongoDB?
- MySQL query to select all fields beginning with a given number with next character a letter?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- Query a nested field within an array with MongoDB
- Find all collections in MongoDB with specific field?
- MongoDB query to find documents with specific FirstName and LastName
- MongoDB query select and display only a specific field from the document?
- MongoDB query to fetch only the “Name” field based on roles?
- MongoDB query to update array with another field?
- MongoDB query (aggregation framework) to match a specific field value

Advertisements