我的json文件如下所示:
{
"_id": ObjectId("57208fa10f80d14e9472942f"),
"title": "How to stop hair loss?",
"postdatetime": "Wed Apr 27 2016 15:38:33 GMT+0530 (IST)",
"status": "Active",
"answer": [{
"answerid": ObjectId("5719b83ffa86ca75d25b600e"),
"text": "l eat protien",
"imageurl": "http://leavesugar.jpg",
"videourl": "http://leavesugar.mp4",
"author": {
"userid": ObjectId("5719b83ffa86ca75d25b600c"),
"name": "chetan kumar",
"imageurl": "http://chetan.jpg",
"followers": 150
},
"postdatetime": "Wed Apr 27 2016 11:55:45 GMT+0530 (IST)",
"status": "Active",
"comment": [{
"commentid": ObjectId("5719b83ffa86ca75d25b600c"),
"parentid": ObjectId("5719b83ffa86ca75d25b600e"),
"posttext": "Most important is workout",
"author": {
"userid": ObjectId("5719b83ffa86ca75d25b600c"),
"name": "raju",
"imageurl": "http://raju.jpg",
},
"postdatetime": "Wed Apr 27 2016 12:17:59 GMT+0530 (IST)"
}]
}, {
"answerid": ObjectId("5719b83ffa86ca75d25b600f"),
"text": "WORKOUT 1 TO 2 HRS DAILY",
"imageurl": "http://workout.jpg",
"videourl": "http://workout.mp4",
"author": {
"userid": ObjectId("5719b83ffa86ca75d25b600a"),
"name": "Ram kumar",
"imageurl": "http://ram.jpg",
},
"postdatetime": "Wed Apr 27 2016 11:56:26 GMT+0530 (IST)",
"status": "Active",
"comment": [{
"commentid": ObjectId("5719b83ffa86ca75d25b600e"),
"parentid": ObjectId("5719b83ffa86ca75d25b600f"),
"posttext": "EAT MORE PROTIEN",
"author": {
"userid": ObjectId("5719b83ffa86ca75d25b600e"),
"name": "raju",
"imageurl": "http://raju.jpg",
},
"postdatetime": "Wed Apr 27 2016 12:49:24 GMT+0530 (IST)"
}, {
"commentid": ObjectId("5719b83ffa86ca75d25b600f"),
"parentid": ObjectId("5719b83ffa86ca75d25b600f"),
"posttext": "EAT PROTIEN AND WORKOUT DAILY",
"author": {
"userid": ObjectId("5719b83ffa86ca75d25b600f"),
"name": "RAMU",
"imageurl": "http://RAMU.jpg",
},
"postdatetime": "Wed Apr 27 2016 12:50:07 GMT+0530 (IST)"
}]
}]
} {
"_id": ObjectId("57204c194694965a19bac484"),
"title": "How to loose weight?",
"postdatetime": "Wed Apr 27 2016 10:50:25 GMT+0530 (IST)",
"status": "Active",
"answer": [{
"answerid": ObjectId("5719b83ffa86ca75d25b600b"),
"text": "leave sugar and eat protien",
"imageurl": "http://leavesugar.jpg",
"videourl": "http://leavesugar.mp4",
"author": {
"userid": ObjectId("5719b83ffa86ca75d25b600c"),
"name": "chetan kumar",
"imageurl": "http://chetan.jpg",
"followers": 250
},
"postdatetime": "Wed Apr 27 2016 11:55:45 GMT+0530 (IST)",
"status": "InActive",
"comment": [{
"commentid": ObjectId("5719b83ffa86ca75d25b600c"),
"parentid": ObjectId("5719b83ffa86ca75d25b600b"),
"posttext": "Most important is workout",
"author": {
"userid": ObjectId("5719b83ffa86ca75d25b600c"),
"name": "raju",
"imageurl": "http://raju.jpg",
},
"postdatetime": "Wed Apr 27 2016 12:17:59 GMT+0530 (IST)"
}]
}, {
"answerid": ObjectId("5719b83ffa86ca75d25b600a"),
"text": "WORKOUT 1 TO 2 HRS DAILY",
"imageurl": "http://workout.jpg",
"videourl": "http://workout.mp4",
"author": {
"userid": ObjectId("5719b83ffa86ca75d25b600a"),
"name": "Ram kumar",
"imageurl": "http://ram.jpg",
},
"postdatetime": "Wed Apr 27 2016 11:56:26 GMT+0530 (IST)",
"status": "Active",
"comment": [{
"commentid": ObjectId("5719b83ffa86ca75d25b600e"),
"parentid": ObjectId("5719b83ffa86ca75d25b600a"),
"posttext": "EAT MORE PROTIEN",
"author": {
"userid": ObjectId("5719b83ffa86ca75d25b600e"),
"name": "raju",
"imageurl": "http://raju.jpg"
},
"postdatetime": "Wed Apr 27 2016 12:49:24 GMT+0530 (IST)"
}, {
"commentid": ObjectId("5719b83ffa86ca75d25b600f"),
"parentid": ObjectId("5719b83ffa86ca75d25b600a"),
"posttext": "EAT PROTIEN AND WORKOUT DAILY",
"author": {
"userid": ObjectId("5719b83ffa86ca75d25b600f"),
"name": "RAMU",
"imageurl": "http://RAMU.jpg"
},
"postdatetime": "Wed Apr 27 2016 12:50:07 GMT+0530 (IST)"
}]
}, {
"answerid": ObjectId("5719b83ffa86ca75d25b600d"),
"text": "WORKOUT 1 TO 2 HRS DAILY",
"imageurl": "http://workout.jpg",
"videourl": "http://workout.mp4",
"author": {
"userid": ObjectId("5719b83ffa86ca75d25b600a"),
"name": "Ram kumar",
"imageurl": "http://ram.jpg"
},
"postdatetime": "Thu Apr 28 2016 10:35:38 GMT+0530 (IST)",
"status": "Active",
"comment": []
}]
}
我想找出" status":" Active"," answer.status":" Active"而且很短 答案根据他们的最大数量。评论
我试着这只发现"状态" 和" answer.status" 处于活动状态,但它显示全部结果" answer.status" 是有效还是有效
如何解决这个问题请帮帮我。
我用这个汇总来找出所有"状态"和" answer.status" :"主动"
db.QuestionAnswer.aggregate([{
"$match" : {
$and : [{
"status" : "Active"
}, {
"answer.status" : "Active"
}
]
}
}
]).pretty()
答案 0 :(得分:1)
要获得有效答案,请使用下面列出的查询:
db.aaa.aggregate([{
$match : {
status : "Active"
}
}, {
$project : {
_id : 1,
title : 1,
postdatetime : 1,
status : 1,
answer : {
$filter : {
input : "$answer",
as : "answe",
cond : {
$eq : ["$$answe.status", "Active"]
}
}
}
}
}
])
输出
{
"_id" : ObjectId("57208fa10f80d14e9472942f"),
"title" : "How to stop hair loss?",
"postdatetime" : "Wed Apr 27 2016 15:38:33 GMT+0530 (IST)",
"status" : "Active",
"answer" : [
{
"answerid" : ObjectId("5719b83ffa86ca75d25b600e"),
"text" : "l eat protien",
"imageurl" : "http://leavesugar.jpg",
"videourl" : "http://leavesugar.mp4",
"author" : {
"userid" : ObjectId("5719b83ffa86ca75d25b600c"),
"name" : "chetan kumar",
"imageurl" : "http://chetan.jpg",
"followers" : 150
},
"postdatetime" : "Wed Apr 27 2016 11:55:45 GMT+0530 (IST)",
"status" : "Active",
"comment" : [
{
"commentid" : ObjectId("5719b83ffa86ca75d25b600c"),
"parentid" : ObjectId("5719b83ffa86ca75d25b600e"),
"posttext" : "Most important is workout",
"author" : {
"userid" : ObjectId("5719b83ffa86ca75d25b600c"),
"name" : "raju",
"imageurl" : "http://raju.jpg"
},
"postdatetime" : "Wed Apr 27 2016 12:17:59 GMT+0530 (IST)"
}
]
},
{
"answerid" : ObjectId("5719b83ffa86ca75d25b600f"),
"text" : "WORKOUT 1 TO 2 HRS DAILY",
"imageurl" : "http://workout.jpg",
"videourl" : "http://workout.mp4",
"author" : {
"userid" : ObjectId("5719b83ffa86ca75d25b600a"),
"name" : "Ram kumar",
"imageurl" : "http://ram.jpg"
},
"postdatetime" : "Wed Apr 27 2016 11:56:26 GMT+0530 (IST)",
"status" : "Active",
"comment" : [
{
"commentid" : ObjectId("5719b83ffa86ca75d25b600e"),
"parentid" : ObjectId("5719b83ffa86ca75d25b600f"),
"posttext" : "EAT MORE PROTIEN",
"author" : {
"userid" : ObjectId("5719b83ffa86ca75d25b600e"),
"name" : "raju",
"imageurl" : "http://raju.jpg"
},
"postdatetime" : "Wed Apr 27 2016 12:49:24 GMT+0530 (IST)"
},
{
"commentid" : ObjectId("5719b83ffa86ca75d25b600f"),
"parentid" : ObjectId("5719b83ffa86ca75d25b600f"),
"posttext" : "EAT PROTIEN AND WORKOUT DAILY",
"author" : {
"userid" : ObjectId("5719b83ffa86ca75d25b600f"),
"name" : "RAMU",
"imageurl" : "http://RAMU.jpg"
},
"postdatetime" : "Wed Apr 27 2016 12:50:07 GMT+0530 (IST)"
}
]
}
]
}