Find mongo documents where a field contains a string pattern
This tutorial guides you to find mongo documents where a field contains a string pattern using mongodb’s regular expression capabilities.
Find mongo documents where a field contains a string
MongoDB uses Perl compatible regular expressions. You need to use $regex and the following syntax option.
{ <field>: { $regex: /pattern/<options> } }
The <options> available to use with regular expression are i(for case insensitivity), m(for patterns that include anchors i.e. ^ for the start and $ for the end), x(extended capability to ignore all white space characters in the regular expressions patterns), s(to allow dot “.” character)
For our example, you can create a sample collection called posts with data using this json. Then try the following commands and check the responses as shown below.
The following example queries all documents where a field contains string pattern “/adv/”
> db.posts.find({tags:{$regex: /adv/}}).pretty()
Response
--------
{
"_id" : ObjectId("5063114bd386d8fadbd6b008"),
"title" : "MongoDB Advantages",
"description" : "Advantages of MongoDB",
"by_user" : "sneppets",
"url" : "https://docs.mongodb.com",
"tags" : [
"#mongodb",
"#mongo_tutor",
"#tutorials",
"#advantages"
],
"likes" : 50,
"comments" : [
{
"user" : "John",
"message" : "Nice tutorial",
"dateCreated" : ISODate("2019-02-25T02:15:00Z"),
"like" : 10
},
{
"user" : "Paul",
"message" : "Good one",
"dateCreated" : ISODate("2019-02-25T02:15:00Z"),
"like" : 12
}
]
}
Further optimization can be done by using “prefix expression” in the regular expression, which means all the potential matches starts with the same string. And this allows MongoDB to construct a “range” from the prefix and find matches against those values from the index within the range.
A regular expression with “prefix expression” starts with caret (^), followed by a string. For example /^#adv/ will be optimized by finding matches against values from the range with index starting with #adv.
The following is example to demonstrate a regular expression with “prefix expression”. Note, “#” in regular expression /^#adv/ is part of the string pattern.
> db.posts.find({tags:{$regex: /^#adv/}}).pretty()
Response
--------
{
"_id" : ObjectId("5063114bd386d8fadbd6b008"),
"title" : "MongoDB Advantages",
"description" : "Advantages of MongoDB",
"by_user" : "sneppets",
"url" : "https://docs.mongodb.com",
"tags" : [
"#mongodb",
"#mongo_tutor",
"#tutorials",
"#advantages"
],
"likes" : 50,
"comments" : [
{
"user" : "John",
"message" : "Nice tutorial",
"dateCreated" : ISODate("2019-02-25T02:15:00Z"),
"like" : 10
},
{
"user" : "Paul",
"message" : "Good one",
"dateCreated" : ISODate("2019-02-25T02:15:00Z"),
"like" : 12
}
]
}
Addition to /^#adv/, the regular expressions /^#adv.*/ and /^#adv.$/ will match equivalent strings but they may have different performance characteristics.
The following example shows how to perform case insensitive regular expression match for the string.
> db.posts.find({tags:{$regex: /^#adv/i}}).pretty()
Response
--------
{
"_id" : ObjectId("5063114bd386d8fadbd6b008"),
"title" : "MongoDB Advantages",
"description" : "Advantages of MongoDB",
"by_user" : "sneppets",
"url" : "https://docs.mongodb.com",
"tags" : [
"#mongodb",
"#mongo_tutor",
"#tutorials",
"#advantages"
],
"likes" : 50,
"comments" : [
{
"user" : "John",
"message" : "Nice tutorial",
"dateCreated" : ISODate("2019-02-25T02:15:00Z"),
"like" : 10
},
{
"user" : "Paul",
"message" : "Good one",
"dateCreated" : ISODate("2019-02-25T02:15:00Z"),
"like" : 12
}
]
}
Also Read
- Find mongoDB documents where an array field exists and not empty
- Guide to list all collections from mongo shell.
- Sort numeric strings as numbers in mongodb.
- Fetch last n records from a collection in mongodb
- How to install only mongo shell client and not mongodb ?
