kianoush dortaj
kianoush dortaj

Reputation: 451

Find item with `Contains` condition using Express and Mongoose

I want to search for items in my collection where the userName contains adm.

I expect to get 2 results, because I have 2 records in my database where the userNames are admin0 and admin2, but it does not return anything.

I'm using this query :

Person
  .find({
    userName: { $in: 'adm'}
  })

What is the problem? How can I search and find persons where the userName contains adm?

Upvotes: 1

Views: 382

Answers (1)

MahanTp
MahanTp

Reputation: 744

You have to use regular expression or $regex when finding the Persons.

Here is the snippet:

Person.find({username: {$regex: '(.*)adm(.*)'}})

Upvotes: 1

Related Questions