Make $elemMatch (projection) return all objects that match criteria

前端 未结 3 1232
死守一世寂寞
死守一世寂寞 2020-12-24 07:09

I will use the example from here

{
 _id: 1,
 zipcode: 63109,
 students: [
              { name: \"john\", school: 102, age: 10 },
              { name: \"je         


        
3条回答
  •  滥情空心
    2020-12-24 08:13

    This worked for me in a case of similar filtering. Now I know this question was asked many years ago, but to anyone looking for an answer like me. here's what worked for me. Thanks to original answer!

    In the case of this particular question: outerparam is zipcode and innerarray.property is students.school.

    let cursor = db
        .collection("somecollection")
        .aggregate(
          { $match: { outerparam: outermatch } },
          { $unwind: "$innerarray" },
          { $match: { "innerarray.property": propertymatch } },
          { $project: { "innerarray.$": 1 } });
    

提交回复
热议问题