以下数据出现在“examSheet”集合
中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']}}}
]);
...
我想返回聚合的json输出,其中名称满足所有条件。 Ex:std:9,年份:2017年,考试:halfyr_Theory with physics marks> 25和 std:9,年份:2017年,考试:Annual_Theory with physics marks> 35
我尝试了不同的方式,如下 符合条件, 能够获得“名称”,但无法再次匹配/提取文档数据。
$in [combined]
尝试在项目之后将halfyr.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'}}]}}]
}
},
{$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"}]}
}
}
有人可以建议如何匹配或过滤掉姓名在半年和年度中常见的位置吗? 在此先感谢!!
期望的输出:
string number = "[\"1\",\"4\"]";
string output = Regex.Replace(number, "[\\[\"\\]]", "");
Console.WriteLine(output);
答案 0 :(得分:2)
db.examSheet.aggregate([
{
"$match":{
"$and":[
{"$or":[
{"exam":"halfyr_T","marks.p":{"$gte":"25"}},
{"exam":"annual_T","marks.p":{"$gte":"35"}}
]},
{"std":"9"},
{"year":"2017"}
]
}
}])
答案 1 :(得分:0)
我使用一些额外的计数来解决它。
感谢您的所有时间和建议!
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}}
]);