查询以获取对象数组中只有一个对象的结果对象

时间:2021-02-27 03:34:59

i need to implement an online survey MEAN stack app. i need to get an document from a collection with only on object inside inv_users array. my document is like this:

我需要实施一个在线调查MEAN堆栈应用程序。我需要从一个集合中获取一个文件,其中只有inv_users数组中的对象。我的文件是这样的:

[
    {
        "_id": "5a431d73fcaf01ec6274ecbc",
        "name": "Patient Satisfaction survey",
        "company_id": "5a432162fcaf01ec6274ec89",
        "start_datetime": "2017-12-11T06:57:44.650Z",
        "end_datetime": "2018-12-11T06:57:44.650Z",
        "logo": "",
        "header_title": "Patient satisfaction Survey",
        "footer_title": "powered by team",
        "img_read_code": "",
        "mail_responded": true,
        "survey_complete": true,
        "inv_users": [
            {
                "email": "jooshipp@gmail.com",
                "ip": "192.168.1.17",
                "latitude": 39.02,
                "longitude": -119.47,
                "survey_completed": false,
                "mail_responsed": false,
                "mail_viewed": true,
                "cmp_user_id": "5a432162fcaf01ec6274ecc0"
            },
            {
                "email": "jamal@gmail.com",
                "ip": "192.168.1.17",
                "latitude": 39.02,
                "longitude": -119.47,
                "survey_completed": false,
                "mail_responsed": false,
                "mail_viewed": true,
                "cmp_user_id": "5a432162fcaf01ec6274ecc8"
            }
        ],
        "answers": [
            {
                "answer": "Satisfied",
                "date_time": "2017-03-24T12:15:12.000Z",
                "ip": "192.168.1.17",
                "latitude": 42.05,
                "longitude": -119.47,
                "global_user_id": "5a431dbafcaf01ec6274ecc0",
                "cmp_user_id": ""
            }
        ],
        "questions": [
            {
                "question": "Are you satisfied with the appearance",
                "ans_type": "Multiple choice",
                "options": [
                    "No",
                    "Satisfied",
                    "a little",
                    "Very Satisfied"
                ]
            }
        ],
        "is_footer": true,
        "is_header": true,
        "display_type": {
            "ui": "single",
            "randamization": true,
            "pageno": true,
            "skip": true,
            "randomization": false
        },
        "category": {
            "id": "5a4167b5aa77271cb7f228ed",
            "name": "Collect Feedback"
        }
    }
]

what my result should look is like this:

我的结果应该是这样的:

[
    {
        "_id": "5a431d73fcaf01ec6274ecbc",
        "name": "Patient Satisfaction survey",
        "company_id": "5a432162fcaf01ec6274ec89",
        "start_datetime": "2017-12-11T06:57:44.650Z",
        "end_datetime": "2018-12-11T06:57:44.650Z",
        "logo": "",
        "header_title": "Patient satisfaction Survey",
        "footer_title": "powered by team",
        "img_read_code": "",
        "mail_responded": true,
        "survey_complete": true,
        "inv_users": [
            {
                "email": "jooshipp@gmail.com",
                "ip": "192.168.1.17",
                "latitude": 39.02,
                "longitude": -119.47,
                "survey_completed": false,
                "mail_responsed": false,
                "mail_viewed": true,
                "cmp_user_id": "5a432162fcaf01ec6274ecc0"
            }
        ],
        "answers": [
            {
                "answer": "Satisfied",
                "date_time": "2017-03-24T12:15:12.000Z",
                "ip": "192.168.1.17",
                "latitude": 42.05,
                "longitude": -119.47,
                "global_user_id": "5a431dbafcaf01ec6274ecc0",
                "cmp_user_id": ""
            }
        ],
        "questions": [
            {
                "question": "Are you satisfied with the appearance",
                "ans_type": "Multiple choice",
                "options": [
                    "No",
                    "Satisfied",
                    "a little",
                    "Very Satisfied"
                ]
            }
        ],
        "is_footer": true,
        "is_header": true,
        "display_type": {
            "ui": "single",
            "randamization": true,
            "pageno": true,
            "skip": true,
            "randomization": false
        },
        "category": {
            "id": "5a4167b5aa77271cb7f228ed",
            "name": "Collect Feedback"
        }
    }
]

i've already written the mongoose query like this:

我已经编写了像这样的mongoose查询:

Survey.find({"_id":req.params.id, "start_datetime": {"$lte": new Date()},"end_datetime": {"$gt": new Date()},inv_users:1, inv_users:{$elemMatch : {  cmp_user_id:user_id}}}, function(err,survey){

            if(survey){
                res.json(survey);
            }
            else if(!survey){

                res.json({
                    status:0,
                    message: "Survey doesn't exist!"
                })  
            }
        });

1 个解决方案

#1


0  

You have mixed match with projection

你有投影的混合匹配

inv_users:1

This in match means match only inv_users whose value is 1. (your intentions might be for project but with output you have asked contradicts this projection)

匹配中的这意味着仅匹配值为1的inv_users。(您的意图可能是针对项目,但是您提出的输出与此预测相矛盾)

also,

也,

The $elemMatch operator limits the contents of an field from the query results to contain only the first element matching the $elemMatch condition.

$ elemMatch运算符将查询结果中字段的内容限制为仅包含与$ elemMatch条件匹配的第一个元素。

This should work in your case

这适用于您的情况

Survey.find({
    "_id"           : req.params.id,
    "start_datetime": {"$lte": new Date()},
    "end_datetime"  : {"$gt": new Date()},
    inv_users       : {
        $elemMatch: {
            cmp_user_id: user_id
        }
    }
},
function (err, survey) {
    if (survey) {
        res.json(survey);
    }
    else if (!survey) {
        res.json({
            status : 0,
            message: "Survey doesn't exist!"
        })
    }
});

#1


0  

You have mixed match with projection

你有投影的混合匹配

inv_users:1

This in match means match only inv_users whose value is 1. (your intentions might be for project but with output you have asked contradicts this projection)

匹配中的这意味着仅匹配值为1的inv_users。(您的意图可能是针对项目,但是您提出的输出与此预测相矛盾)

also,

也,

The $elemMatch operator limits the contents of an field from the query results to contain only the first element matching the $elemMatch condition.

$ elemMatch运算符将查询结果中字段的内容限制为仅包含与$ elemMatch条件匹配的第一个元素。

This should work in your case

这适用于您的情况

Survey.find({
    "_id"           : req.params.id,
    "start_datetime": {"$lte": new Date()},
    "end_datetime"  : {"$gt": new Date()},
    inv_users       : {
        $elemMatch: {
            cmp_user_id: user_id
        }
    }
},
function (err, survey) {
    if (survey) {
        res.json(survey);
    }
    else if (!survey) {
        res.json({
            status : 0,
            message: "Survey doesn't exist!"
        })
    }
});