返回只有匹配的子文档的所有文档(Mongodb)

时间:2019-12-18 11:30:00

标签: mongodb

我有一个文档,例如:

{
    "STATUS": "SUCCESS",
    "DATA": [{
        "user_age": 29,
        "user_gender": "Male",
        "_id": "5df8952146bcc95a0b8116d3",
        "user_full_name": "Arnab Mondal",
        "user_email_id": "xxx@xyz.com",
        "user_mobile_no": 1234567890,
        "login_credential": [
            {
                "user_login_username": "xxxx.yyyy",
                "user_login_password": "$2b$10$DAetR3gD.da59V9JJIm.uePHMxVRanrQfZMr4Ot5P0ex3EkyZQt0S",
                "_id": "5df8952146bcc95a0b8116d9",
                "status": "INACTIVE"`enter code here`
            },
            {
                "user_login_username": "xxxx.yyyy",
                "user_login_password": "$2b$10$gCIRzjzVKLA6l3vQ6rf2l.Nm8OyI3QEUUF.t.LAlFvzLWkoh/C/Zq",
                "_id": "5df8954846bcc95a0b8116da",
                "status": "INACTIVE"
            },
            {
                "user_login_username": "xxxx.yyyy",
                "user_login_password": "$2b$10$H4rwn7ytSYTJMOWBDGwrF.pf0joURDDZSwIj7a9YZrLupdvXSLNIy",
                "_id": "5df8a11e46bcc95a0b8116db",
                "status": "ACTIVE"
            }
        ],
        "status": "ACTIVE",
        "__v": 0
    }]
}

我已经尝试过以下查询:

usersModel.aggregate([{ $match: { 'login_credential.status': 'ACTIVE' }},{ $project: {login_credential: { $filter: {input: '$login_credential',as: 'login_credential', cond: { $eq: ['$$login_credential.status', 'ACTIVE']}}}}}]).exec().then( results => {res.json({'STATUS': 'SUCCESS', 'DATA': results});}).catch(err => {res.json({'STATUS': 'ERROR', 'ERROR': err});});

我希望这样的输出:

{
    "STATUS": "SUCCESS",
    "DATA": [{
        "user_age": 29,
        "user_gender": "Male",
        "_id": "5df8952146bcc95a0b8116d3",
        "user_full_name": "Arnab Mondal",
        "user_email_id": "xxx@xyz.com",
        "user_mobile_no": 1234567890,
        "login_credential": [
            {
                "user_login_username": "xxxx.yyyy",
                "user_login_password": "$2b$10$H4rwn7ytSYTJMOWBDGwrF.pf0joURDDZSwIj7a9YZrLupdvXSLNIy",
                "_id": "5df8a11e46bcc95a0b8116db",
                "status": "ACTIVE"
            }
        ],
        "status": "A",
        "__v": 0
    }]
}

但是查询的输出中还包含不匹配的对象。

0 个答案:

没有答案