Find document with array that contains a specific value

前端 未结 10 1340
猫巷女王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:15

    Though agree with find() is most effective in your usecase. Still there is $match of aggregation framework, to ease the query of a big number of entries and generate a low number of results that hold value to you especially for grouping and creating new files.

      PersonModel.aggregate([
                { 
                     "$match": { 
                         $and : [{ 'favouriteFoods' : { $exists: true, $in: [ 'sushi']}}, ........ ]  }
                 },
                 { $project : {"_id": 0, "name" : 1} }
                ]);
    

提交回复
热议问题