How to query MongoDB with "like"

like operator

MongoDB doesn’t support the like operator which is common in SQL.

We can achieve the same functionality of the like operator using the find() function and regexes.

Syntax

db.users.insert({name: 'john'})
db.users.insert({name: 'johnny'})
db.users.insert({name: 'sam'})

Usecase 1: Contains a string

The SQL statement with the like operator is as follows:

select * from table where name like '%jo%';

In MongoDB,

db.users.find({name: /jo/})

Usecase 2: Starting with a string

The SQL statement with the like operator is as follows:

select * from table where name like 'jo%';

In MongoDB,

db.users.find({name: /^jo/})

Usecase 3: Ending with a string

The SQL statement with the like operator is as follows:

select * from table where name like '%jo';

In MongoDB,

db.users.find({name: /jo$/})

Let’s try the above commands in the terminal below.

Terminal 1
Terminal
Loading...

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved