26

我将使用此处的示例

{
 _id: 1,
 zipcode: 63109,
 students: [
              { name: "john", school: 102, age: 10 },
              { name: "jess", school: 102, age: 11 },
              { name: "jeff", school: 108, age: 15 }
           ]
}
{
 _id: 2,
 zipcode: 63110,
 students: [
              { name: "ajax", school: 100, age: 7 },
              { name: "achilles", school: 100, age: 8 },
           ]
}

{
 _id: 3,
 zipcode: 63109,
 students: [
              { name: "ajax", school: 100, age: 7 },
              { name: "achilles", school: 100, age: 8 },
           ]
}

{
 _id: 4,
 zipcode: 63109,
 students: [
              { name: "barney", school: 102, age: 7 },
           ]
}

如果我跑

db.schools.find( { zipcode: 63109 },
             { students: { $elemMatch: { school: 102 } } } )

它将给出每个数组的第一个结果。命名为:

{ "_id" : 1, "students" : [ { "name" : "john", "school" : 102, "age" : 10 } ] }
{ "_id" : 3 }
{ "_id" : 4, "students" : [ { "name" : "barney", "school" : 102, "age" : 7 } ] }

我怎样才能让它返回符合条件的数组的所有对象(而不仅仅是第一个对象)?这意味着:

{
 _id: 1,
 students: [
              { name: "john", school: 102, age: 10 },
              { name: "jess", school: 102, age: 11 }
           ]
}    
{ _id: 3 }
{_id: 4, students: [ { name: "barney", school: 102, age: 7 }]}
4

3 回答 3

22

为了返回多个子文档,您将需要使用聚合框架。这将返回您要查找的所有子文档:

db.zip.aggregate(
  {$match: {zipcode: 63109}},
  {$unwind: "$students"},
  {$match: {"students.school": 102}}
)

您可以做各种事情来获得不同的输出,但这将返回:

{
    "result" : [
        {
            "_id" : 1,
            "zipcode" : 63109,
            "students" : {
                "name" : "john",
                "school" : 102,
                "age" : 10
            }
        },
        {
            "_id" : 1,
            "zipcode" : 63109,
            "students" : {
                "name" : "jess",
                "school" : 102,
                "age" : 11
            }
        },
        {
            "_id" : 4,
            "zipcode" : 63109,
            "students" : {
                "name" : "barney",
                "school" : 102,
                "age" : 7
            }
        }
    ],
    "ok" : 1
}
于 2013-10-31T02:25:10.740 回答
2

以前的和不正确的答案:

这应该从今天开始工作。请参阅https://docs.mongodb.com/v3.2/reference/operator/projection/positional/#array-field-limitations

在查询中使用查询并在投影中公开子文档时,您应该得到正确的结果,$elemMatch如下所示:

db.schools.find( { zipcode: 63109, students: { $elemMatch: { school: 102 } } },
                 { 'students.$': 1 } )


新答案

到目前为止,无法使用find(). 请aggregate()取而代之或采取以下可能性之一:

您可以通过在投影中添加数组属性来获取匹配文档的所有子文档:

db.schools.find( { zipcode: 63109, students: { $elemMatch: { school: 102 } } }, { 'students': 1 })
> { "_id" : 1, "students" : [ { "name" : "john", "school" : 102, "age" : 10 }, { "name" : "jess", "school" : 102, "age" : 11 }, { "name" : "jeff", "school" : 108, "age" : 15 } ] }
> { "_id" : 4, "students" : [ { "name" : "barney", "school" : 102, "age" : 7 } ] }

或者,您可以获取与$elemMatch子文档上的查询匹配的第一项:

db.schools.find( { zipcode: 63109, students: { $elemMatch: { school: 102 } } }, { 'students.$': 1 })
> { "_id" : 1, "students" : [ { "name" : "john", "school" : 102, "age" : 10 } ] }
> { "_id" : 4, "students" : [ { "name" : "barney", "school" : 102, "age" : 7 } ] }
于 2017-08-13T14:54:26.600 回答
0

在类似过滤的情况下,这对我有用。现在我知道这个问题是多年前提出的,但对于任何像我这样寻找答案的人来说。这对我有用。感谢原始答案!

在这个特定问题的情况下: outerparamiszipcodeinnerarray.propertyis students.school

let cursor = db
    .collection("somecollection")
    .aggregate(
      { $match: { outerparam: outermatch } },
      { $unwind: "$innerarray" },
      { $match: { "innerarray.property": propertymatch } },
      { $project: { "innerarray.$": 1 } });
于 2020-06-06T23:00:37.187 回答