Find document with array that contains a specific value

前端 未结 10 1471
猫巷女王i
猫巷女王i 2020-11-22 04:06

If I have this schema...

person = {
    name : String,
    favoriteFoods : Array
}

... where the favoriteFoods array is popula

10条回答
  •  野的像风
    2020-11-22 04:32

    I feel like $all would be more appropriate in this situation. If you are looking for person that is into sushi you do :

    PersonModel.find({ favoriteFood : { $all : ["sushi"] }, ...})
    

    As you might want to filter more your search, like so :

    PersonModel.find({ favoriteFood : { $all : ["sushi", "bananas"] }, ...})
    

    $in is like OR and $all like AND. Check this : https://docs.mongodb.com/manual/reference/operator/query/all/

提交回复
热议问题