
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB Query to Search for String Like Email in Field Values
Search for email string using MongoDB find(). Let us create a collection with documents −
> db.demo727.insertOne({UserId:"[email protected]"}); { "acknowledged" : true, "insertedId" : ObjectId("5eab375f43417811278f5898") } > db.demo727.insertOne({UserId:"[email protected]"}); { "acknowledged" : true, "insertedId" : ObjectId("5eab376043417811278f5899") } > db.demo727.insertOne({UserId:"[email protected]"}); { "acknowledged" : true, "insertedId" : ObjectId("5eab376143417811278f589a") }
Display all documents from a collection with the help of find() method −
> db.demo727.find();
This will produce the following output −
{ "_id" : ObjectId("5eab375f43417811278f5898"), "UserId" : "[email protected]" } { "_id" : ObjectId("5eab376043417811278f5899"), "UserId" : "[email protected]" } { "_id" : ObjectId("5eab376143417811278f589a"), "UserId" : "[email protected]" }
Following is the query to search for @email like string −
> db.demo727.find({"UserId":/@email/i});
This will produce the following output −
{ "_id" : ObjectId("5eab375f43417811278f5898"), "UserId" : "[email protected]" } { "_id" : ObjectId("5eab376143417811278f589a"), "UserId" : "[email protected]" }
Advertisements