I want to find using the object value

I’m sorry. I may not be very good at English, so my explanation might be inadequate. Please understand

I want to search for the approver_id within the document_approval_line_array using the document_approver_idx value.

{
    "_id" : ObjectId("64cc5a9501b7f613484ca9f9"),
    "document_submitter" : "woong",
    "document_approval_line_id" : ObjectId("64ca0ab82cd80b2168a44736"),
    "document_approval_line_array" : [
        [
            {
                "approver_type" : "2",
                "approver_id" : "woong",
                "approver_name" : "",
                "approver_department" : "IT본부",
                "approver_title" : "사원",
                "approver_shownas" : "테스터"
            }
        ],
        [
            {
                "approver_type" : "2",
                "approver_id" : "denny",
                "approver_name" : "데니",
                "approver_department" : "IT본부",
                "approver_title" : "대리",
                "approver_shownas" : "UI/UX팀"
            }
        ],
        [
            {
                "approver_type" : "1",
                "approver_id" : "reggie",
                "approver_name" : "레지",
                "approver_department" : "IT본부",
                "approver_title" : "팀장",
                "approver_shownas" : "개발팀장"
            },
            {
                "approver_type" : "1",
                "approver_id" : "luke",
                "approver_name" : "루크",
                "approver_department" : "IT본부",
                "approver_title" : "사원",
                "approver_shownas" : "테스터"
            }
        ],
        [
            {
                "approver_type" : "0",
                "approver_id" : "test",
                "approver_name" : "대장",
                "approver_department" : "IT본부",
                "approver_title" : "대표이사",
                "approver_shownas" : "대표이사"
            }
        ]
    ],
    "document_approver_idx" : NumberInt(2),
    "document_approver_nextIdx" : NumberInt(3)

}
db.getCollection("company_document").aggregate([
            {
              $addFields: {
                temp: {
                  $arrayElemAt: ['$document_approval_line_array', '$document_approver_idx']
                }
              }
            },
            {
              $match: {
                'temp.approver_id': 'reggie'
              }
            }
          ]
)

Although this query works correctly, but I don’t want to use addFields. Is it possible?

Hello, @reggie_han and welcome to the community! :grinning:

From the description and examples you provided it is clear, that your aggregation pipeline perform $match using the calculated data. For that you need additional stage, like $addFields or $project. If you want to omit those stages, you may want to add some changes to your model. For example:

db.approvals.insertMany([
  {
    _id: 'A',
    document_approval_line_array: [
      [
        {
          "approver_id": "M1",
        },
        {
          "approver_id": "M2",
        }
      ],
      [
        {
          "approver_id": "M3"
        }
      ]
    ],
    document_approver_idx: NumberInt(1),
    document_approver_ids: ['M3'] // new field
  },
  {
    _id: 'B',
    document_approval_line_array: [
      [
        {
          "approver_id": "N1",
        }
      ],
      [
        {
          "approver_id": "N2",
        },
        {
          "approver_id": "N3",
        }
      ],
    ],
    document_approver_idx: NumberInt(1),
    document_approver_ids: ['N2, N3'] // new field
  },
]);

Later you can achieve same results by using this aggregation pipeline:

db.approvals.aggregate([
  {
    $match: {
      document_approver_ids: 'N3'
    }
  }
]);

Or even with a simple find operation:

db.approvals.find({
  document_approver_ids: 'N3'
});

Both above methods would return the same result:

[
  {
    _id: 'B',
    document_approval_line_array: [
      [ { approver_id: 'N1' } ],
      [ { approver_id: 'N2' }, { approver_id: 'N3' } ]
    ],
    document_approver_idx: 1,
    document_approver_ids: [ 'N2', 'N3' ]
  }
]
3 Likes