簡體   English   中英

獲取 object 與 mongoose 中子數組中的匹配對象

[英]get object with the matching objects in sub array in mongoose

數據庫中的數據存儲:

{
    "_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此查詢有效,但未返回結果 object 而是返回兩個對象。

對於sub_question數組中的兩個對象,我必須匹配 question_id 以及(obj and obj1)的答案

  • $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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM