mongodb 多个匹配条件和 return 个具有通用名称的文档

mongodb multiple match conditions and return documents with common name

以下数据存在于 "examSheet" 集合中

{"name":"a1", "std":"9", "year":"2017", "exam":"halfyr_T", "marks":[{"p":"45","m":"40","c":"50"}]}
{"name":"a1", "std":"9", "year":"2017", "exam":"halfyr_P", "marks":[{"p":"40","m":"28","c":"38"}]}
{"name":"a1", "std":"9", "year":"2017", "exam":"annual_T", "marks":[{"p":"40","m":"50","c":"48"}]}
{"name":"a1", "std":"9", "year":"2017", "exam":"annual_P", "marks":[{"p":"45","m":"42","c":"18"}]}
{"name":"a2", "std":"9", "year":"2017", "exam":"halfyr_T", "marks":[{"p":"25","m":"30","c":"50"}]}
{"name":"a2", "std":"9", "year":"2017", "exam":"halfyr_P", "marks":[{"p":"41","m":"48","c":"28"}]}
{"name":"a2", "std":"9", "year":"2017", "exam":"annual_T", "marks":[{"p":"30","m":"48","c":"24"}]}
{"name":"a2", "std":"9", "year":"2017", "exam":"annual_P", "marks":[{"p":"35","m":"08","c":"38"}]}
{"name":"b1", "std":"10", "year":"2017", "exam":"halfyr_T","marks":[{"p":"45","m":"40","c":"50"}]}
{"name":"b1", "std":"10", "year":"2017", "exam":"halfyr_P", "marks": [{"p":"40","m":"28","c":"38"}]}
{"name":"b1", "std":"10", "year":"2017", "exam":"annual_T", "marks": [{"p":"40","m":"50","c":"48"}]}
{"name":"b1", "std":"10", "year":"2017", "exam":"annual_P", "marks": [{"p":"45","m":"42","c":"18"}]}
{"name":"b2", "std":"10", "year":"2017", "exam":"halfyr_T", "marks": [{"p":"25","m":"30","c":"50"}]}
{"name":"b2", "std":"10", "year":"2017", "exam":"halfyr_P", "marks": [{"p":"41","m":"48","c":"28"}]}
{"name":"b2", "std":"10", "year":"2017", "exam":"annual_T", "marks": [{"p":"30","m":"48","c":"24"}]}
{"name":"b2", "std":"10", "year":"2017", "exam":"annual_P", "marks": [{"p":"35","m":"08","c":"38"}]}

...

我想 return 汇总 json 输出,其中名称满足所有条件。 例如:std:9、year:2017、exam:halfyr_Theory 物理分数 > 25 并且 std:9 , year:2017, 考试: annual_Theory 物理分数 > 35

我尝试了如下不同的方法, 与条件匹配, 能够获取'name',但无法再次匹配/提取文档数据。

db.examSheet.aggregate([{$facet: {
    'halfyr': [ {$match: {$and: [{'exam': 'halfyr_T'},{'std': '9'},{'year': "2017"},{'marks.p': {$gte: '25'}}]}}],
    "annual": [ {$match: {$and: [{'exam': 'annual_T'},{'std': '9'},{'year': "2017"},{'marks.p': {$gte: '35'}}]}}] 
    }
},
{$project: {'_id': 0, "combined": {$setIntersection: ['$halfyr.name', '$annual.name']}}}
                    ]);

已尝试,在项目后将 halfyr.name 与 $in [combined] 匹配,等等。 但无法解决问题。

请帮助或建议我解决这个问题。

我试过这种方法。

db.examSheet.aggregate([{$facet: {
                            "halfyr": [ {$match: {$and: [{'exam': 'halfyr_T'},{'std': '9'},{'year': "2017"},{'marks.p': {$gte: '25'}}]}}],
                            "annual": [ {$match: {$and: [{'exam': 'annual_T'},{'std': '9'},{'year': "2017"},{'marks.p': {$gte: '35'}}]}}] 
                            }
                    },
                    {$unwind : "$annual"}
                ]);

我的输出是:

{
    "halfyr" : [
            {
                    "_id" : ObjectId("5a98c639b0ae80e6c6a92031"),
                    "name" : "a1",
                    "std" : "9",
                    "year" : "2017",
                    "exam" : "halfyr_T",
                    "marks" : [
                            {
                                    "p" : "45",
                                    "m" : "40",
                                    "c" : "50"
                            }
                    ]
            },
            {
                    "_id" : ObjectId("5a98c639b0ae80e6c6a9203e"),
                    "name" : "a2",
                    "std" : "9",
                    "year" : "2017",
                    "exam" : "halfyr_T",
                    "marks" : [
                            {
                                    "p" : "25",
                                    "m" : "30",
                                    "c" : "50"
                            }
                    ]
            }
    ],
    "annual" : {
            "_id" : ObjectId("5a98c639b0ae80e6c6a92038"),
            "name" : "a1",
            "std" : "9",
            "year" : "2017",
            "exam" : "annual_T",
            "marks" : [
                    {
                            "p" : "40",
                            "m" : "50",
                            "c" : "48"
                    }
            ]
    }

}

任何人都可以建议如何匹配或过滤出半年和一年中常见的名字吗? 提前致谢!!

期望的输出:

{
"name":"a1", "std":"9", "year":"2017",
"halfyr" : {"exam":"halfyr_T", "marks": [{"p":"45","m":"40","c":"50"}]},
"annual" : {"exam":"annual_T", "marks": [{"p":"40","m":"50","c":"48"}]}
}

您可以使用 $or. inside the $and.

db.examSheet.aggregate([
 {
  "$match":{
    "$and":[
      {"$or":[
        {"exam":"halfyr_T","marks.p":{"$gte":"25"}},
        {"exam":"annual_T","marks.p":{"$gte":"35"}}
      ]},
      {"std":"9"},
      {"year":"2017"}
    ]
 }
}])

我使用一些额外的文档计数解决了这个问题。

感谢大家的时间和建议!

db.examSheet.aggregate([
 {
  "$match":{
    "$and":[
      {"$or":[
        {"exam":"halfyr_T","marks.p":{"$gte":"25"}},
        {"exam":"annual_T","marks.p":{"$gte":"35"}}
      ]},
      {"std":"9"},
      {"year":"2017"}
    ]
 }},
 {$group: {
        "_id": {
            "code": "$name",
            "type": { "$cond": [ 
                { "$and":[
                    { "$gte": [ "$marks.p", 25 ] },
                    { "$eq": [ "$exam", "halfyr_T" ] }
                ]},
                "A",
                "B"
            ]}
        },
        "all_data" : {$addToSet : "$$ROOT"}
    }},
        // Simply add up the results for each "type"
    { "$group": {
        "_id": "$_id.code",
        "all_data" : {$addToSet : "$all_data"},
        "score": { "$sum": 1 }
    }},
        // Now filter to keep only results with score 2
    { "$match": { "score": 2 }},
    {$project : {_id :0 , all_data : 1}}
]);