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.

2

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks very much ! I need to go through aggregations thoroughly.
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 ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.