Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning an array in mongodb

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
            }
        ]
    }
]
}
like image 922
Antespo Avatar asked Aug 10 '26 01:08

Antespo


1 Answers

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'},
    }},
])
like image 95
2 revs, 2 users 67% Avatar answered Aug 11 '26 14:08

2 revs, 2 users 67%



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!