Qui-Gon Jinn
Qui-Gon Jinn

Reputation: 4388

MongoDB Mongoose: Passing array in find query

I do my first project using MongoDB and Mongoose.

I wonder if is possible to use array in find query to retrieve all objects corresponding to array elements.

Please see the dummy code bellow.


let allCategory = ["a", "b", "c", "d"];

static async getItemByCategory(allCategory ) {
     const item = Item.find({ allCategory });
     return item;
}
        

Upvotes: 0

Views: 2324

Answers (1)

Juanes Velasquez
Juanes Velasquez

Reputation: 140

You can use the $in operator

Item.find({ category : { $in : allCategory }, ...});

or $all depends on your need

Item.find({ category : { $all : allCategory }, ...});

Please checkout Mongo query documentation

Upvotes: 3

Related Questions