Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (47.6k points)

If I have this schema...

person = {

     name : String, 

     favoriteFoods : Array 

}

where the favoriteFoods array is populated with strings. How can I find all persons that have "sushi" as their favourite food using mongoose?

I was hoping for something along the lines of:

PersonModel.find({ favoriteFoods : { $contains : "sushi" }, function(...) {...});

(I know that there is no $contains in MongoDB, just explaining what I was expecting to find before knowing the solution)

1 Answer

0 votes
by (106k points)

To find a document with an array that contains a specific value you can use the below-mentioned way:-

You can make the string array explicit in your schema by using the following query:

person = { name : String, favouriteFoods : [String] }

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...