資料庫中的資料存盤:
{
"_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
}
我從 StackOverflow 上的用戶(用戶:14732669)那里得到的答案是:https ://mongoplayground.net/p/pAV76ctwhfC 這個查詢正在運行,但沒有回傳結果物件,而是回傳兩個物件。
我必須為sub_question陣列中的兩個物件匹配 question_id 以及(obj 和 obj1)的答案
uj5u.com熱心網友回復:
$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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/390974.html
標籤:数组 MongoDB 猫鼬 mongodb-查询
上一篇:如何使用索引并匹配兩列?
