查询以获取对象数组中只有一个对象的结果对象
Posted
技术标签:
【中文标题】查询以获取对象数组中只有一个对象的结果对象【英文标题】:query to get result object with only one object inside an array of objects 【发布时间】:2018-06-14 01:49:32 【问题描述】:我需要实现一个在线调查 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!"
)
);
【问题讨论】:
【参考方案1】:你与投影混合匹配
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!"
)
);
【讨论】:
以上是关于查询以获取对象数组中只有一个对象的结果对象的主要内容,如果未能解决你的问题,请参考以下文章