0

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;
}
        

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer Juanes!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.