
- 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
How to query an Array[String] for a Regular Expression match?
To query an array string for a regexp match, use the following syntax
db.yourCollectionName.find( { yourFieldName: /yourStartingValue./ } ).pretty();
Let us first create a collection with documents
> db.queryArrayDemo.insertOne({"StudentFullName":["Carol Taylor","Caroline Williams","Claire Brown"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2774c6304881c5ce84ba0") } > db.queryArrayDemo.insertOne({"StudentFullName":["John Smith","Jace Doe","Jabin Brown"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ca277b36304881c5ce84ba1") }
Following is the query to display all documents from a collection with the help of find() method
> db.queryArrayDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5ca2774c6304881c5ce84ba0"), "StudentFullName" : [ "Carol Taylor", "Caroline Williams", "Claire Brown" ] } { "_id" : ObjectId("5ca277b36304881c5ce84ba1"), "StudentFullName" : [ "John Smith", "Jace Doe", "Jabin Brown" ] }
Here is how you can query an array string for a regexp match
> db.queryArrayDemo.find( { StudentFullName : /J./ } ).pretty();
This will produce the following output
{ "_id" : ObjectId("5ca277b36304881c5ce84ba1"), "StudentFullName" : [ "John Smith", "Jace Doe", "Jabin Brown" ] }
- Related Articles
- How to match a regular expression against a string?
- How do we use Python regular expression to match a date string?
- Program to match vowels in a string using regular expression in Java
- How to match at the beginning of string in python using Regular Expression?
- How to match parentheses in Python regular expression?
- How to match a word in python using Regular Expression?
- How to match a whitespace in python using Regular Expression?
- MongoDB regular expression to match a specific record?
- How to match digits using Java Regular Expression (RegEx)
- How to match a nonwhitespace character in python using Regular Expression?
- How to match a single character in python using Regular Expression?
- How to use regular expression in Java to pattern match?
- How to write a Python regular expression to match multiple words anywhere?
- How to match at the end of string in python using Regular Expression?\n\n
- How to match only digits in Python using Regular Expression?

Advertisements