ryndshn
ryndshn

Reputation: 701

Mongoose use array in find().where()

I have an array of Strings that I want to use as a parameter for finding documents in my mongodb.

Basically what I want to do is

// Using query builder
Person.
  find().
  where('name.last').equals('Ghost' || 'ETC' || 'ETC')....

But instead of using a bunch of or statements, I want to just pass my array of Strings.

Is this possible?

Upvotes: 1

Views: 3563

Answers (1)

raina77ow
raina77ow

Reputation: 106385

Just use .in method, taking $in operator in play:

Person.
  find().
  where('name.last').in(['Ghost', 'Foo', 'Bar'])

Quoting the MongoDB docs:

The $in operator selects the documents where the value of a field equals any value in the specified array.

Upvotes: 2

Related Questions