Reputation: 4388
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
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