mongoDB数组文档搜索

mongoDB array document search

这是一个非常简单的问题,我搜索了所有,但找不到答案

我只有

 {
"_id" : 1,
    "name" : {
        "first" : "John",
        "last" : "Backus"
    },
 "awards" : [
        {
            "award" : "W.W. McDowell Award",
            **"year" : 1967,**
            "by" : "IEEE Computer Society"
        },
        {
            "award" : "Draper Prize",
            **"year" : 1967,**
            "by" : "National Academy of Engineering"
        },
        {
            "award" : "National Medal of Science",
            "year" : 1975,
            "by" : "National Science Foundation"
        }
    ]
},
 {
 "_id" : 2,
    "name" : {
        "first" : "John",
        "last" : "McCarthy"
    },
"awards" : [
        {
            "award" : "Turing Award",
            "year" : 1971,
            "by" : "ACM"
        },
        {
            "award" : "Kyoto Prize",
            **"year" : 1967,**
            "by" : "Inamori Foundation"
        },
        {
            "award" : "National Medal of Science",
            "year" : 1990,
            "by" : "National Science Foundation"
        }
    ]
},

我要的是1967年获得的所有奖项!

我试过了db.bios.find({"awards.year":1967},{award.$}).pretty()

我只得到

"_id" : 1,
"awards" : [
        {
            "award" : "W.W. McDowell Award",
            "year" : 1967,
            "by" : "IEEE Computer Society"
        },

],

但我需要在所有文档中找到(第一个有两个所以总共 3 个像

{
"_id" : 1,
    "awards" : [
            {
                "award" : "W.W. McDowell Award",
                "year" : 1967,
                "by" : "IEEE Computer Society"
            },
             {
                "award" : "Draper Prize",
                **"year" : 1967,**
                "by" : "National Academy of Engineering"
            }
    ]
},
{
    "_id" : 2,
    "award":[{
                "award" : "Kyoto Prize",
                "year" : 1967,
                "by" : "Inamori Foundation"
            }
    ]
}

我尝试使用 $all 但无法得到结果。我希望我清楚 是的,我正在使用 bios 示例。 这一定很简单,我在某处做空了 请帮忙。

您必须使用 aggregation framework,因为 $ 仅指第一个匹配项。对于您的情况,以下可能有效:

db.bios.aggregate( [ 
    { $match : {"awards.year":1967} }, 
    { $unwind : "$awards"}, 
    { $match : {"awards.year" :1967} } 
]);

屈服

{
        "_id" : 1,
        "name" : {
                "first" : "John",
                "last" : "Backus"
        },
        "awards" : {
                "award" : "W.W. McDowell Award",
                "year" : 1967,
                "by" : "IEEE Computer Society"
        }
}
{
        "_id" : 1,
        "name" : {
                "first" : "John",
                "last" : "Backus"
        },
        "awards" : {
                "award" : "Draper Prize",
                "year" : 1967,
                "by" : "National Academy of Engineering"
        }
}
{
        "_id" : 2,
        "name" : {
                "first" : "John",
                "last" : "McCarthy"
        },
        "awards" : {
                "award" : "Kyoto Prize",
                "year" : 1967,
                "by" : "Inamori Foundation"
        }
}

如您所见,这将复制文档的其余部分(因此 John Backus 的文档返回了两次),但您可以通过添加 $group 来更改它,例如

db.bios.aggregate( [ { $match : {"awards.year":1967} },     
  { $unwind : "$awards"},      
  { $match : {"awards.year" :1967} }, 
  { $group : { _id : "$_id", "awards" : { $push : "$awards" } } } 
]).pretty();

但是有很多方法可以做到这一点,所以我建议您深入研究文档以获得您需要的投影。