返回mongodb中的某些字段

时间:2015-03-06 21:32:06

标签: python mongodb pymongo

我正在尝试返回符合我标准的数组。这就是我所拥有的:

{
"_id": 1,
"awardAmount": 20000,
"url": "www.url.com",
"numAwards": 2,
"award": "Faculty Research Grant",
"Type": "faculty",
"Applicants": [
    {
        "preAwards": "NO1",
        "Name": "Omar1",
        "School": "SCSU1",
        "citizenship": "YES1",
        "budget": 1,
        "Advisor": "Dr. DaPonte1",
        "Major": "CSC1",
        "appId": 100,
        "Research": "Test data entry1",
        "Time": "12 months1",
        "URL": "www.url.com",
        "Evaluators": [
            {
                "abstractScore": 11,
                "evalNum": 1,
                "goalsObjectivesScore": 11
            },
            {
                "abstractScore": 22,
                "evalNum": 2,
                "goalsObjectivesScore": 22
            }
        ]
    },
    {
        "preAwards": "NO2",
        "citizenship": "YES2",
        "Major": "CSC2",
        "Time": "12 months2",
        "budget": 2,
        "URL": "www.2.com",
        "appId": 200,
        "Advisor": "Dr. DaPonte2",
        "Name": "Omar2",
        "Research": "Test data entry2",
        "School": "SCSU2",
        "url": "www.2.com"
    },
    {
        "preAwards": "NO3",
        "citizenship": "YES3",
        "Major": "CSC3",
        "Time": "12 months3",
        "budget": 3,
        "URL": "www.3.com",
        "appId": 300,
        "Advisor": "Dr. DaPonte3",
        "Name": "Omar3",
        "Research": "Test data entry3",
        "School": "SCSU3",
        "url": "www.3.com",
        "Evaluators": [
            {
                "abstractScore": 454,
                "evalNum": 1,
                "goalsObjectivesScore": 4546
            }
        ]
    }
]
}

我想回复那些没有评估者字段的申请人。

{
"_id": 1,
"awardAmount": 20000,
"url": "www.url.com",
"numAwards": 2,
"award": "Faculty Research Grant",
"Type": "faculty",
"Applicants": [
    {
        "preAwards": "NO2",
        "citizenship": "YES2",
        "Major": "CSC2",
        "Time": "12 months2",
        "budget": 2,
        "URL": "www.2.com",
        "appId": 200,
        "Advisor": "Dr. DaPonte2",
        "Name": "Omar2",
        "Research": "Test data entry2",
        "School": "SCSU2",
        "url": "www.2.com"
    }
]
}

这只是一个文档的示例。我希望所有申请人在所有文件中都没有评估者字段。

3 个答案:

答案 0 :(得分:1)

在mongo shell中你可以这样做:

db.test.find(
{
    Applicants : { $elemMatch : {  "Evaluators" : { $exists : 0 } }}
}, 
{
    "_id" : 1,
    "awardAmount" : 1,
    "url" : 1,
    "numAwards" : 2,
    "award" : 1,
    "Type" : 1,
    'Applicants.$' : 1,         
});

一个问题是上面的查询只返回一个Applicants,其中没有Evaluators,有效的完整解决方案将通过聚合实现

db.test.aggregate(
[
   { $match : { Applicants : { $elemMatch : {  "Evaluators" : { $exists : 0 } } } } },
   { $unwind : "$Applicants" },
   { $match : { "Applicants.Evaluators" : { $exists : 0 } } },
   {
       $group : 
       {   
           _id : '$_id', 
           Applicants : { $push : '$Applicants' }, 
           awardAmount : { $first : '$awardAmount' } ,
           url : { $first : '$url' } ,
           numAwards : { $first : '$numAwards' } ,
           award : { $first : '$award' } ,
           Type : { $first : '$Type' } ,
       } 
   }
]  
)

答案 1 :(得分:1)

将汇总与pymongo

一起使用
col.aggregate([{"$unwind": "$Applicants"}, {"$match" : {"Applicants.Evaluators": {"$exists": False}}}]))

<强>输出

{'ok': 1.0,
 'result': [{'Applicants': {'Advisor': 'Dr. DaPonte2',
                            'Major': 'CSC2',
                            'Name': 'Omar2',
                            'Research': 'Test data entry2',
                            'School': 'SCSU2',
                            'Time': '12 months2',
                            'URL': 'www.2.com',
                            'appId': 200,
                            'budget': 2,
                            'citizenship': 'YES2',
                            'preAwards': 'NO2',
                            'url': 'www.2.com'},
             'Type': 'faculty',
             '_id': 1,
             'award': 'Faculty Research Grant',
             'awardAmount': 20000,
             'numAwards': 2,
             'url': 'www.url.com'}]}

答案 2 :(得分:0)

如果我正确理解您的问题,我建议您使用汇总管道$unwind“申请人”字段中的文件。然后,您可以使用$match过滤生成的文档,以删除“评估者”所在的文档,然后使用$group$first$push重新组合在一起。希望这有一些帮助。