1

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.

Saud Khan
  • 123
  • 4
  • I think you should check this http://stackoverflow.com/questions/21623979/mongodb-aggregation-framework-match-by-nested-documents or find more about mongo aggregation. – Neo-coder Mar 25 '15 at 09:46
  • I read http://docs.mongodb.org/manual/tutorial/query-documents/#match-a-field-in-the-embedded-document-using-the-array-index but this example does not have mutiple values in an array – Saud Khan Mar 25 '15 at 09:50

1 Answers1

1

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.

mnemosyn
  • 45,391
  • 6
  • 76
  • 82
  • Thanks very much ! I need to go through aggregations thoroughly. – Saud Khan Mar 25 '15 at 10:04
  • Thanks I can find the exact ones now by adding $project and $sort. After finding the exact element , is there a way to update it? II mean what If I want to add a new item in the award array where name.first=john ? – Saud Khan Mar 26 '15 at 09:54