3

I'm trying to return certain elements of an array in the document below.

{
"_id": 2,
"awardAmount": 6000,
"url": "www.url.com",
"numAwards": 3,
"award": "Faculty Seed Research Grant",
"Type": "faculty",
"Applicates": [
    {
        "School": "psu",
        "Name": "tom",
        "URL": "www.url.com",
        "Time": "",
        "Research": "",
        "Budge": 7500,
        "appId": 100,
        "citizenship": "us",
        "Major": "mat",
        "preAwards": "None",
        "Advisor": ""
    },
    {
        "School": "ffff",
        "Name": "KEVIN",
        "URL": "www.url.com",
        "Time": "5/5/5-6/6/6",
        "Research": "topology",
        "Budge": 9850,
        "appId": 101,
        "citizenship": "us",
        "Major": "gym",
        "preAwards": "None",
        "Advisor": "Dr. cool",
        "Evaluators": [
            {
                "abstractScore": 3,
                "goalsObjectivesScore": 4,
                "evalNum": 1
            },
            {
                "abstractScore": 545646,
                "goalsObjectivesScore": 46546,
                "evalNum": 2
            }
        ]
    }
]
}

I want only the "Applicates" data if they have an "Evaluators" field. Here is what I was trying

db.coll.find({'Applicates.Evaluators':{'$exists': True }})

This gives me the whole document but I just want "Applicates" data that have the "Evaluators" field in it like this.

{
"_id": 2,
"awardAmount": 6000,
"url": "www.url.com",
"numAwards": 3,
"award": "Faculty Seed Research Grant",
"Type": "faculty",
"Applicates": [
    {
        "School": "ffff",
        "Name": "KEVIN",
        "URL": "www.url.com",
        "Time": "5/5/5-6/6/6",
        "Research": "topology",
        "Budge": 9850,
        "appId": 101,
        "citizenship": "us",
        "Major": "gym",
        "preAwards": "None",
        "Advisor": "Dr. cool",
        "Evaluators": [
            {
                "abstractScore": 3,
                "goalsObjectivesScore": 4,
                "evalNum": 1
            },
            {
                "abstractScore": 545646,
                "goalsObjectivesScore": 46546,
                "evalNum": 2
            }
        ]
    }
]
}

1 Answer 1

3

Try this (the key is using $unwind operator)

db.coll.aggregate(
[
{ $match : {'Applicates.Evaluators':{'$exists': true }} },
{ $unwind : "$Applicates" },
{ $match : {'Applicates.Evaluators':{'$exists': true }} },
{ $group : { _id : "$_id", 
    'Applicates' : {$push : '$Applicates'} , 
    awardAmount : {$first : '$awardAmount'},
    url : {$first : '$url'},
    award : {$first : '$award'},
    numAwards : {$first : '$numAwards'},
    award : {$first : '$award'},
    Type : {$first : '$Type'},
    }},
])
Sign up to request clarification or add additional context in comments.

Comments

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.