mongodb嵌套数组匹配和排序

mongodb嵌套数组匹配和排序,mongodb,Mongodb,我的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("5719b83ffa86

我的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”“answer.status”处于活动状态,但它会显示其“answer.status”处于活动或非活动状态的所有结果

如何解决这个问题请帮助我

我使用此聚合查找所有“状态”和“应答状态”:“活动”


要仅获取活动答案,请使用下面列出的查询:

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"]
                        }
                    }
                }
            }
        }
    ])
输出


您需要使用聚合。如果您使用的是mongodb 3.2,那么有一个简单的聚合查询。我使用这个db.QuestionAnswer.aggregate([{“$match”:{$and:[{“status”:“Active”},{“answer.status”:“Active”}]}])。pretty()请提供预期的文档示例首先,我只需要那些“status”:“Active”和“answer.status”:“Active”的数据在上面的json文件中,我使用了db.QuestionAnswer.aggregate([{“$match”:{$and:[{“status”:“Active”},{“answer.status”:“Active”}}}]).pretty()Bt它显示所有“status”:“Active”的数据,但不只是显示那些“answer.status”:“Active”的数据,它还显示那些“answer.status”:“InActive”的文件。在上面的json文件中,谢谢您的帮助
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)"
                }
            ]
        }
    ]
}