在MongoDB中如何查询count number of 2 equal field?
How can query in MongoDB that count number of 2 equal field?
我的调查结果 (name=surveyresults
) 在 MongoDB 中有一个 collection。我想要一个查询,根据类别给我正确答案的数量,例如,类别 "Bee" 正确答案的数量 10。
我尝试了不同的方法,但这些不是我想要的结果。
我已经搜索并发现这个 post Group count with MongoDB using aggregation framework 有用但不适合我。
这是我 surveyResults
collection 中的部分数据:
[{"_id":"0eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOjE5LCJpYXQiOjE1MjQwMDgzOTl9.2YvhnXtCD7-fm4B14k10m6NF7xuv7moCTbekVekkbvY","category":"Wasp","photo":"A_wasp_565","description":"","answer":"Bee","__v":0},{"_id":"1eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOjE5LCJpYXQiOjE1MjQwMDgzOTl9.2YvhnXtCD7-fm4B14k10m6NF7xuv7moCTbekVekkbvY","category":"Wasp","photo":"A_Pompilid_wasp_007","description":"","answer":"Wasp","__v":0},{"_id":"2eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOjE5LCJpYXQiOjE1MjQwMDgzOTl9.2YvhnXtCD7-fm4B14k10m6NF7xuv7moCTbekVekkbvY","category":"Wasp","photo":"wasp_248","description":"","answer":"Wasp","__v":0},{"_id":"3eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOjE5LCJpYXQiOjE1MjQwMDgzOTl9.2YvhnXtCD7-fm4B14k10m6NF7xuv7moCTbekVekkbvY","category":"Fly","photo":"A_butterfly_291","description":"kjlkjlkjlk","answer":"Moth/Butterfly","__v":0},
我想要这样的结果:
[{"category":"Fly","count":3, "correct":1},{"category":"Wasp","count":3, "correct":1},{"category":"Moth/Butterfly","count":4, "correct":2},{"category":"Bee","count":3, "correct":1}]
现在我有这两个查询但没有给我正确的结果:
1.
SurveyResults.aggregate([
{ $group: {
_id: { answer: '$answer', category: '$category' }
}},
{ $group: {
_id: '$_id.answer',
answer_correct: { $sum: 1 }
}},
{ $project: {
_id: 0,
answer: '$_id',
answer_correct: 1
}}
]).exec(callback);
2.
SurveyResults.aggregate([
{
$group:{
_id:"$answer",
count: { $sum : {$cond : { if: { $eq: ["answer", "$category"]}, then: 1, else: 0} }}
}
}]).exec(callback);
此外,我可以通过此查询获得基于类别的答案数量:
SurveyResults.aggregate([
{
$group:{
_id:"$answer",
count: { $sum : 1 }
}
}]).exec(callback);
结果:
[{"_id":"Don't know","count":2},{"_id":"Fly","count":3},{"_id":"Wasp","count":3},{"_id":"Moth/Butterfly","count":4},{"_id":"Bee","count":3}]
这是你想要的:
SurveyResults.aggregate([
$group: {
_id: "$category",
"count": { $sum: 1 }, // simply count all questions per category
"correct": {
$sum: { // and sum up the correct ones in a field called "correct"
$cond: [ // ...where "correct ones" means
{ $eq: [ "$category", "$answer" ] }, // that "category" needs to match "answer"
1,
0
]
}
}
}
}, {
$project: { // this is just to effectively rename the "_id" field into "category" - may or may not be needed
_id: 0,
"category": "$_id",
"count": "$count",
"correct": "$correct"
}
}]).exec(callback);
我的调查结果 (name=surveyresults
) 在 MongoDB 中有一个 collection。我想要一个查询,根据类别给我正确答案的数量,例如,类别 "Bee" 正确答案的数量 10。
我尝试了不同的方法,但这些不是我想要的结果。
我已经搜索并发现这个 post Group count with MongoDB using aggregation framework 有用但不适合我。
这是我 surveyResults
collection 中的部分数据:
[{"_id":"0eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOjE5LCJpYXQiOjE1MjQwMDgzOTl9.2YvhnXtCD7-fm4B14k10m6NF7xuv7moCTbekVekkbvY","category":"Wasp","photo":"A_wasp_565","description":"","answer":"Bee","__v":0},{"_id":"1eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOjE5LCJpYXQiOjE1MjQwMDgzOTl9.2YvhnXtCD7-fm4B14k10m6NF7xuv7moCTbekVekkbvY","category":"Wasp","photo":"A_Pompilid_wasp_007","description":"","answer":"Wasp","__v":0},{"_id":"2eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOjE5LCJpYXQiOjE1MjQwMDgzOTl9.2YvhnXtCD7-fm4B14k10m6NF7xuv7moCTbekVekkbvY","category":"Wasp","photo":"wasp_248","description":"","answer":"Wasp","__v":0},{"_id":"3eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOjE5LCJpYXQiOjE1MjQwMDgzOTl9.2YvhnXtCD7-fm4B14k10m6NF7xuv7moCTbekVekkbvY","category":"Fly","photo":"A_butterfly_291","description":"kjlkjlkjlk","answer":"Moth/Butterfly","__v":0},
我想要这样的结果:
[{"category":"Fly","count":3, "correct":1},{"category":"Wasp","count":3, "correct":1},{"category":"Moth/Butterfly","count":4, "correct":2},{"category":"Bee","count":3, "correct":1}]
现在我有这两个查询但没有给我正确的结果:
1.
SurveyResults.aggregate([
{ $group: {
_id: { answer: '$answer', category: '$category' }
}},
{ $group: {
_id: '$_id.answer',
answer_correct: { $sum: 1 }
}},
{ $project: {
_id: 0,
answer: '$_id',
answer_correct: 1
}}
]).exec(callback);
2.
SurveyResults.aggregate([
{
$group:{
_id:"$answer",
count: { $sum : {$cond : { if: { $eq: ["answer", "$category"]}, then: 1, else: 0} }}
}
}]).exec(callback);
此外,我可以通过此查询获得基于类别的答案数量:
SurveyResults.aggregate([
{
$group:{
_id:"$answer",
count: { $sum : 1 }
}
}]).exec(callback);
结果:
[{"_id":"Don't know","count":2},{"_id":"Fly","count":3},{"_id":"Wasp","count":3},{"_id":"Moth/Butterfly","count":4},{"_id":"Bee","count":3}]
这是你想要的:
SurveyResults.aggregate([
$group: {
_id: "$category",
"count": { $sum: 1 }, // simply count all questions per category
"correct": {
$sum: { // and sum up the correct ones in a field called "correct"
$cond: [ // ...where "correct ones" means
{ $eq: [ "$category", "$answer" ] }, // that "category" needs to match "answer"
1,
0
]
}
}
}
}, {
$project: { // this is just to effectively rename the "_id" field into "category" - may or may not be needed
_id: 0,
"category": "$_id",
"count": "$count",
"correct": "$correct"
}
}]).exec(callback);