How to fetch only specified array element from nested array present in document

I have data in MongoDB like below:

{
    "channel" : {
        "_id" : "1",
        "name" : "switch",
        "formats" : [ 
            {
                "formatName" : "ISO8583-93",
                "description" : "ISO Format",
                "fields" : [ 
                    {
                        "name" : "0",
                        "alias" : "MTI",
                        "lenght" : "4",
                        "description" : "",
                        "type" : "FIXED",
                        "dataType" : "",
                        "required" : true
                    }
                ],
                "messages" : [ 
                    {
                        "name" : "balanceEnquiry",
                        "alias" : "balanceEnquiry",
                        "description" : "balanceEnquiry Request  :  Sender Bank -> MessageHub",
                        "messageIdentification" : "",
                        "messageType" : "",
                        "messageFormat" : "",
                        "fields" : [ 
                            {
                                "name" : "DE_0",
                                "alias" : "MTI",
                                "lenght" : "4",
                                "description" : "",
                                "type" : "FIXED",
                                "dataType" : ""
                            }, 
                            {
                                "name" : "DE_1",
                                "alias" : "Primary Bitmap",
                                "lenght" : "8",
                                "description" : "Primary Bitmap",
                                "type" : "BIN",
                                "dataType" : ""
                            }
                        ]
                    }, 
                    {
                        "name" : "fundTransfer",
                        "alias" : "creditTransfer",
                        "description" : "Funds Transfer Request  :  Sender Bank -> Message Hub",
                        "messageIdentification" : "",
                        "messageType" : "",
                        "messageFormat" : "",
                        "fields" : [ 
                            {
                                "name" : "DE_0",
                                "alias" : "MTI",
                                "lenght" : "4",
                                "description" : "",
                                "type" : "FIXED",
                                "dataType" : ""
                            }, 
                            {
                                "name" : "DE_1",
                                "alias" : "Primary Bitmap",
                                "lenght" : "8",
                                "description" : "Primary Bitmap",
                                "type" : "BIN",
                                "dataType" : ""
                            }
                        ]
                    }
                ]
            }, 
            {
                "formatName" : "ISO20022",
                "description" : "",
                "fields" : [ 
                    {
                        "name" : "0",
                        "alias" : "MTI",
                        "lenght" : "4",
                        "description" : "",
                        "type" : "FIXED",
                        "dataType" : "",
                        "required" : true
                    }, 
                    {
                        "name" : "1",
                        "alias" : "Bitmap(s)",
                        "lenght" : "8",
                        "description" : "",
                        "type" : "BIN",
                        "dataType" : "",
                        "required" : true
                    }
                ]
            }
        ]
    }
}

I want to fetch element of array “messages” by its “name”. Depending on given condition where “channel.name”:“switch” and “channel.formats.formatName”:“ISO8583-93” and “channel.formats.messages.name”:“balanceEnquiry”. I am expecting only below part as a output

 {
                        "name" : "balanceEnquiry",
                        "alias" : "balanceEnquiry",
                        "description" : "balanceEnquiry Request  :  Sender Bank -> MessageHub",
                        "messageIdentification" : "",
                        "messageType" : "",
                        "messageFormat" : "",
                        "fields" : [ 
                            {
                                "name" : "DE_0",
                                "alias" : "MTI",
                                "lenght" : "4",
                                "description" : "",
                                "type" : "FIXED",
                                "dataType" : ""
                            }, 
                            {
                                "name" : "DE_1",
                                "alias" : "Primary Bitmap",
                                "lenght" : "8",
                                "description" : "Primary Bitmap",
                                "type" : "BIN",
                                "dataType" : ""
                            }
                        ]
                    }

How to get this specified result by MongoDB query?

1 Like

Hi @Erica_01,

I think the following aggregation should work:

 db.coll.aggregate([{$match: {
“channel.name”:“switch”, “channel.formats.formatName”:“ISO8583-93” ,“channel.formats.messages.name”:“balanceEnquiry”
}},
{
      $project: {
         results: {
            $filter: {
               input: "$channel.formats.messages",
               as: "message",
               cond: { $eq: [ "$$message.name", "balanceEnquiry" ] }
            }
         }
      }
   },
{ $replaceRoot: { newRoot: "$results" } }]);

Haven’t tested the full logic, but this should help.

Best regards
Pavel

Hello @Erica_01, welcome to the community.

Here is some information to start with about querying an array and projecting specific fields from the returned document.

Hello, @Erica_01! Welcome to the community!

Ok, so you need to query the document and modify the output.
To achieve that you need to use and aggregation.

Example:

db.test1.aggregate([
  {
    // first, filter the documents, that contain
    // fields with necessary values
    $match: {
      'channel.name': 'switch',
      'channel.formats.formatName': 'ISO8583-93',
      'channel.formats.messages.name': 'balanceEnquiry',
    },
  },
  // the following $unwind stages will convert your arrays
  // to objects, so it would be easier to filter the messages
  {
    $unwind: '$channel.formats',
  },
  {
    $unwind: '$channel.formats.messages',
  },
  {
    // filter messages here
    $match: {
      'channel.formats.messages.name': 'balanceEnquiry',
    },
  },
  {
    // returns only message(s)
    $replaceWith: '$channel.formats.messages',
  },
]).pretty();
4 Likes

Thank you so much Slava!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.