- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHPPhysics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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" }Advertisements