Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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"
]
} Advertisements
