我需要实施一个在线调查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"
}
}
]
我的结果应该是这样的:
[
{
"_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"
}
}
]
我已经写过像这样的猫鼬查询:
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!"
})
}
});
答案 0 :(得分:0)
您与投影混合匹配
inv_users:1
这在匹配中意味着只匹配值为1的inv_users。(您的意图可能是针对项目,但是您提出的输出与此预测相矛盾)
也
$ elemMatch运算符将查询结果中字段的内容限制为仅包含与$ elemMatch条件匹配的第一个元素。
这适用于您的情况
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!"
})
}
});