Saud Khan
Saud Khan

Reputation: 123

mongoDB array document search

This is a very simple question , I searched all, but could'nt find the answer

all I have is

 {
"_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"
        }
    ]
},

All I need is all the awards that have been won in year 1967!

and I tried db.bios.find({"awards.year":1967},{award.$}).pretty()

and I get only

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

],

but I need to find in all of the documents (the firsdt one has two so totalyy 3 like

{
"_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"
            }
    ]
}

I tried using $all but couldnt get the result. I hope im clear and yes, I was using the bios example. This must be pretty simple, Im getting short somwhere Please help.

Upvotes: 1

Views: 100

Answers (1)

mnemosyn
mnemosyn

Reputation: 46341

You'll have to use the aggregation framework, because $ only refers to the first match. In your case, the following might work:

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

Yielding

{
        "_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"
        }
}

As you can see, that will duplicate the rest of the document (so John Backus's document is returned twice), but you can change that by adding a $group, e.g.

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

But there's a lot of ways to do that, so I suggest you dig into the documentation to get the projection you need.

Upvotes: 1

Related Questions