在猫鼬的子数组中获取具有匹配对象的对象
Posted
技术标签:
【中文标题】在猫鼬的子数组中获取具有匹配对象的对象【英文标题】:get object with the matching objects in sub array in mongoose 【发布时间】:2022-01-24 05:45:45 【问题描述】:数据库中的数据存储:
"_id": "61c1c0efc204bb170e280d2f",
"title": "Searching Relevant Cases",
"question": "Your Industry",
"vendor_id": "61b8324040fb21d80f3e6702",
"answer_type": "is_radio",
"optional_answer": false,
"sub_question_type": "none",
"view": "list_view",
"is_active": true,
"is_delete": false,
"sub_question": [],
"draggable_list": [],
"__v": 0
,
"_id": "61c1c142c204bb170e280d45",
"title": "Searching Relevant Cases",
"question": "Employee size",
"vendor_id": "61b8324040fb21d80f3e6702",
"answer_type": "is_radio",
"optional_answer": false,
"sub_question_type": "none",
"view": "list_view",
"is_active": true,
"is_delete": false,
"sub_question": [],
"draggable_list": [],
"__v": 0
,
"_id": "61c1c24ac204bb170e280d5e",
"title": "Dependent",
"question": "On previous Chosen option",
"vendor_id": "61b8324040fb21d80f3e6702",
"answer_type": "is_radio",
"optional_answer": false,
"sub_question_type": "has_parent",
"sub_question": [
"question_id": "61c1c0efc204bb170e280d2f",
"_id": "61c1c24ac204bb170e280d5f",
"answer": "Bank"
,
"question_id": "61c1c142c204bb170e280d45",
"_id": "61c1c24ac204bb170e280d60",
"answer": "SMB (<100)"
],
"view": "list_view",
"is_active": true,
"is_delete": false,
"draggable_list": [],
"__v": 0
,
"_id": "61c1c2d7c204bb170e280d7e",
"title": "Dependent 1",
"question": "On previous Chosen option 1",
"vendor_id": "61b8324040fb21d80f3e6702",
"answer_type": "is_radio",
"optional_answer": false,
"sub_question_type": "has_parent",
"sub_question": [
"question_id": "61c1c142c204bb170e280d45",
"_id": "61c1c2d7c204bb170e280d7f",
"answer": "Mid (100 - 1000)"
,
"question_id": "61c1c0efc204bb170e280d2f",
"_id": "61c1c2d7c204bb170e280d80",
"answer": "Tech"
],
"view": "list_view",
"is_active": true,
"is_delete": false,
"draggable_list": [],
"__v": 0
查询参数为:
obj:
"question_id": "61c1c0efc204bb170e280d2f",
"answer": "Bank"
,
obj1:
"question_id": "61c1c142c204bb170e280d45",
"answer": "SMB (<100)"
我期待的结果是:
"_id": "61c1c24ac204bb170e280d5e",
"title": "Dependent",
"question": "On previous Chosen option",
"vendor_id": "61b8324040fb21d80f3e6702",
"answer_type": "is_radio",
"optional_answer": false,
"sub_question_type": "has_parent",
"sub_question": [
"question_id": "61c1c0efc204bb170e280d2f",
"_id": "61c1c24ac204bb170e280d5f",
"answer": "Bank"
,
"question_id": "61c1c142c204bb170e280d45",
"_id": "61c1c24ac204bb170e280d60",
"answer": "SMB (<100)"
],
"view": "list_view",
"is_active": true,
"is_delete": false,
"draggable_list": [],
"__v": 0
我在 *** 上从用户(用户:14732669)那里得到的答案是: https://mongoplayground.net/p/pAV76ctwhfC 此查询正在运行,但不返回结果对象,而是返回两个对象。
我必须为 sub_question 数组中的两个对象匹配 question_id 以及 (obj and obj1) 的答案
【问题讨论】:
【参考方案1】:$match
db.collection.aggregate([
"$match":
"$and": [
"sub_question.question_id": "61c1c0efc204bb170e280d2f",
"sub_question.answer": "Bank"
,
"sub_question.question_id": "61c1c142c204bb170e280d45",
"sub_question.answer": "SMB (<100)"
]
])
mongoplayground
【讨论】:
@MunamRamay 接受这个答案,投票会很棒。以上是关于在猫鼬的子数组中获取具有匹配对象的对象的主要内容,如果未能解决你的问题,请参考以下文章