爱喝酒的刺猬 · USA奥特曼形象变化历程:美女奥特曼贝斯颜值 ...· 2 月前 · |
拉风的黄瓜 · 诺基亚世界大会2011:Windows ...· 5 月前 · |
要出家的吐司 · 中华人民共和国传染病防治法-· 1 年前 · |
愉快的铁链 · MSU Denver Institute ...· 1 年前 · |
宽容的水煮肉 · 《欢迎来到实力至上主义的教室第二季》更新至第 ...· 1 年前 · |
MongoDB provides the functionality to search a pattern in a string during a query by writing a regular expression . A regular expression is a generalized way to match patterns with sequences of characters. MongoDB uses Perl compatible regular expressions(PCRE) version 8.42 along with UTF-8 support. In MongoDB, we can do pattern matching in two different ways:
This operator provides regular expression capabilities for pattern matching stings in the queries. Or in other words, this operator is used to search for the given string in the specified collection. It is helpful when we don’t know the exact field value that we are looking in the document. For example, a collection containing 3 documents i.e.,
and we are looking for developer information. So, with the help of the $regex operator, we create a pattern(i.e., {position: {$regex: “developer”}}) that will return only those documents that contain developer string.
Important Points:
Syntax:
{ <field>: { $regex: /pattern/, $options: '<options>' } } { <field>: { $regex: 'pattern', $options: '<options>' } }
$options:
In MongoDB, the following <options> are available for use with regular expression:
Examples:
In the following examples, we are working with:
Database: GeeksforGeeks
Collection: employee
Documents: Six documents that contain the details of the employees in the form of field-value pairs.
db.employee.find({position : {$regex : "developer"}}).pretty()
Here, we are displaying the documents of those employees whose position field contain developer string. So we pass a regular expression using $regex operator(i.e. {$regex : “developer”}) for the position field in the find() method.
db.employee.find({position:{$regex:"software",$options:"i"}}).pretty()
Here, we are displaying the documents of those employees whose position field contain case-insensitive “software” string. So, we pass a regular expression with option(i.e., {$regex : “software”, $options: “$i”}) for the position field in the find() method. In the regular expression, $options:”$i” is used to match both lower case and upper case pattern in the given string(i.e., “software”).
db.employee.find({Name:{$regex:"^B"}}).pretty()
Here, we are displaying the documents of those employees whose name starts with ‘B’ letter. So, we pass a regular expression using $regex operator(i.e. {$regex : “^B”}) for the Name field in the find() method.
db.employee.find({Name:{$regex:"e$"}}).pretty()
Here, we are displaying the documents of those employees whose names end with the ‘e’ letter. So, we pass a regular expression using $regex operator(i.e. {$regex : “e$”}) for the Name field in the find() method.
db.employee.find({Name: /te/}).pretty()
Here, we are displaying the documents of those employees whose names contain the “te” string. So, we pass a regular expression(i.e., {Name: /te/}) for the Name field in the find() method. In this regular expression, // means to specify your search criteria in between these delimiters, i.e., /te/.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now !
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
要出家的吐司 · 中华人民共和国传染病防治法- 1 年前 |