
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
Set Regex in MongoDB
Let us create a collection with documents −
> db.demo675.insertOne({ ... "ListOfNames":["John","Chris","David"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f5b404263e90dac943ef") } > db.demo675.insertOne({ "ListOfNames":["Bob","Johnson","Sam"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f5c804263e90dac943f0") } > db.demo675.insertOne({ "ListOfNames":["Jace","Sam"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f5d304263e90dac943f1") }
Display all documents from a collection with the help of find() method −
> db.demo675.find();
This will produce the following output −
{ "_id" : ObjectId("5ea3f5b404263e90dac943ef"), "ListOfNames" : [ "John", "Chris", "David" ] } { "_id" : ObjectId("5ea3f5c804263e90dac943f0"), "ListOfNames" : [ "Bob", "Johnson", "Sam" ] } { "_id" : ObjectId("5ea3f5d304263e90dac943f1"), "ListOfNames" : [ "Jace", "Sam" ] }
Following is the query to query with regexp and array −
> db.demo675.find({"ListOfNames": {$in:[/John/,/J/]}});
This will produce the following output −
{ "_id" : ObjectId("5ea3f5b404263e90dac943ef"), "ListOfNames" : [ "John", "Chris", "David" ] } { "_id" : ObjectId("5ea3f5c804263e90dac943f0"), "ListOfNames" : [ "Bob", "Johnson", "Sam" ] } { "_id" : ObjectId("5ea3f5d304263e90dac943f1"), "ListOfNames" : [ "Jace", "Sam" ] }
Advertisements