mongodbHow to use regex in MongoDB?
MongoDB supports the use of regular expressions to search for strings within documents. To use regex in MongoDB, the $regex
operator is used in a find()
query.
For example, the following query will find all documents in the users
collection where the name
field contains the string John
:
db.users.find({name: {$regex: /John/}})
The code above consists of the following parts:
db.users.find()
: This is thefind()
query used to search for documents in theusers
collection.{name: {$regex: /John/}}
: This is the condition used to search for documents where thename
field contains the stringJohn
. The$regex
operator is used to specify that a regular expression should be used for the search. The regular expression/John/
is used to search for the stringJohn
.
For more information, see the MongoDB documentation.
More of Mongodb
- How to empty an array in MongoDB?
- How to use watch in MongoDB?
- How to use MongoDB query with "or" condition?
- How to kill an operation in MongoDB?
- How to use eq in MongoDB?
- How to use MongoDB HTTP interface?
- How to check the version of MongoDB?
- How to implement one-to-many relation in MongoDB?
- How to work with time series data in MongoDB?
- How to use unwind in MongoDB?
See more codes...