Lightning21Cheetah
Lightning21Cheetah

Reputation: 51

MongoDB: How Find the document where their name starts with “S” and “R”?

I use this manually db.employees.find({Name: /^S/}); and db.employees.find({Name: /^R/}); to find the starts letter of "S" and "R",

is their any way to use the two starts letter in one function?

Upvotes: 4

Views: 5706

Answers (2)

Dhruvi Pathak
Dhruvi Pathak

Reputation: 1

This explicitly uses the $regex operator. It searches for all names that start with "S". It is a more flexible approach because you can add additional regex options like case insensitivity ($options: 'i').

db.Employees.find({name: {$regex: /^S/}});

and

db.Employees.find({name: {$regex: /^s/, $options: 'i'}}); 

Upvotes: 0

turivishal
turivishal

Reputation: 36134

You can use | pipe sign to put or condition in regex,

db.employees.find({
  Name: /^S|^R/
})

Playground

Upvotes: 7

Related Questions