Home > Database > Mysql Tutorial > How to Replicate SQL's 'LIKE' Query Functionality in MongoDB?

How to Replicate SQL's 'LIKE' Query Functionality in MongoDB?

DDD
Release: 2025-01-23 19:22:10
Original
787 people have browsed it

How to Replicate SQL's

MongoDB's Equivalent to SQL's LIKE Clause

SQL's LIKE operator simplifies pattern-based string searches. MongoDB offers similar functionality using regular expressions.

Using Regular Expressions for Pattern Matching:

To mimic SQL's LIKE, employ MongoDB's regular expression operator. For instance, to find strings containing "m":

/.*m.*/
Copy after login

Here, . matches any character, * matches zero or more occurrences. This expression finds "m" anywhere within the string.

Example Query:

To retrieve all users with names including "m":

db.users.find({ name: /.*m.*/ })
Copy after login

Simplified Approach (for simple cases):

For basic "contains" searches, a simpler regex suffices:

/m/
Copy after login

Important Note:

MongoDB's regular expressions are more versatile than SQL's LIKE. They allow for sophisticated and precise pattern definitions.

Further Reading:

The above is the detailed content of How to Replicate SQL's 'LIKE' Query Functionality in MongoDB?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template