如何查询可从嵌套数组mongodb mongoose

时间:2018-08-11 17:17:15

标签: arrays mongodb mongoose mean-stack

如何查询对象数组,这些对象可以从嵌套数组返回所有匹配的对象。我需要添加多个条件,例如要查询任何特定ObjectId的数组。简而言之,我的查询应该是这样-

query = {     “ userID”:“ useridxx”,     “ instituteId”:“ instituteIdxxx”,     “ students.isActive”:“ Y”   }

现在,如果我运行此查询,这将向我返回学生数组中的所有记录,包括“ isActive”:“ N”,但我希望仅包含“ isActive”:“ Y”的记录。

我的MongoDB集合中有以下数据。我想获取所有具有 isActive:“ Y” 学生记录。关于如何为此编写MongoDB查询的任何线索。我在应用程序中使用猫鼬。

{
"_id": {
    "$oid": "5b6ef6e6fdadde29843d20bc"
},
"userID": "amit@gmail.com",
"instituteId": "5b65577b0136492a7445494e",
"batchId": "5b6ed56e8964ca052c62ad5c",
"students": [
    {
        "name": "test",
        "fatherName": "father name",
        "dob": {
            "$date": "2018-08-10T18:30:00.000Z"
        },
        "email": "test@gmail",
        "mobile": 1234567890,
        "course": "course name",
        "courseFee": 1234567,
        "paidFee": 123,
        "dueFee": 1234444,
        "registrationDate": {
            "$date": "2018-08-11T14:47:02.510Z"
        },
        "createdDate": {
            "$date": "2018-08-11T14:47:02.510Z"
        },
        "_id": {
            "$oid": "5b6ef6e6fdadde29843d20bd"
        },
        "batch": {
            "batchId": "5b6ed56e8964ca052c62ad5c"
        },
        "isActive": "Y"
    },
    {
        "createdDate": {
            "$date": "2018-08-11T14:48:33.829Z"
        },
        "registrationDate": {
            "$date": "2018-08-11T14:48:33.829Z"
        },
        "dueFee": 12000,
        "paidFee": 121,
        "courseFee": 12121,
        "course": "course name ",
        "mobile": 123456,
        "email": "del@gmail",
        "dob": {
            "$date": "2018-08-07T18:30:00.000Z"
        },
        "fatherName": "deleted",
        "name": "deleteed student",
        "_id": {
            "$oid": "5b6ef742671ab520bc596986"
        },
        "batch": {
            "batchId": "5b6ed56e8964ca052c62ad5c"
        },
        "isActive": "N"
    },
    {
        "createdDate": {
            "$date": "2018-08-11T16:15:29.409Z"
        },
        "registrationDate": {
            "$date": "2018-08-11T16:15:29.409Z"
        },
        "dueFee": 1212000,
        "paidFee": 121,
        "courseFee": 1212121,
        "course": "vefsdrwcds",
        "mobile": 1123456543,
        "email": "st@fgg",
        "dob": {
            "$date": "2018-08-10T18:30:00.000Z"
        },
        "fatherName": "wrecdsaz",
        "name": "st",
        "_id": {
            "$oid": "5b6f0ba12e0eab2b48c3b596"
        },
        "batch": {
            "batchId": "5b6ed56e8964ca052c62ad5c"
        },
        "isActive": "Y"
    }
],
"__v": 0

}

我必须使用多个条件来搜索查询-

query = {
"userID": "useridxx",
"instituteId": "instituteIdxxx",
"students.isActive": "Y"
}

预期输出是-学生中有两个记录/对象,其中有“ isActive”:“ Y”。

我的问题不是this的重复项,我想获取与任何特定userId和InstituteId都匹配的“学生”对象。回答的问题在查询中没有这样的条件。简而言之,我不想浏览所有馆藏的文档并获取所有“不活跃”的学生,而是想要获取任何特定“ UserId”和“ InstituteId”的不活跃的学生。

0 个答案:

没有答案