How to update only specified array elements from nested array present in document

I have data in MongoDB like below:

{
    "channel" : {
        "_id" : "Object ID ",
        "name" : "switch",
        "formats" : [ 
            {
                "_id" : "Object ID ",
                "formatName" : "ISO8583-93",
                "description" : "ISO Format",
                "fields" : [ 
                    {
                        "name" : "0",
                        "alias" : "MTI",
                        "lenght" : "4",
                        "description" : "",
                        "type" : "FIXED",
                        "dataType" : "",
                        "required" : true
                    }
                ],
                "messages" : [ 
                    {    "_id" : "Object ID ",
                        "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" : ""
                            }
                        ]
                    }, 
                    {    "_id" : "Object ID ",
                        "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" : ""
                            }
                        ]
                    }
                ]
            }, 
            {     "_id" : "Object ID ",
                "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 update element of array “messages” by its “_id”. Depending on given condition where “channel.name”:“switch” and “channel.formats.formatName”:“ISO8583-93” and “channel.formats.messages._id”:“Object Id" of balance enquiry ”. I want to update only below part,I dont want to update field only…want to replace complete object of mongodb through java

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

How do I update this from Java?

1 Like

Hello
I was thinking to send you this from yesterday but you said i want to get that part
of the document,and i answered to the previous question.
If you want to update use this query,that updates the message if
(and (= channel.name “switch”)
(= format.formatName “ISO8583-93”)
(= message.name “balanceEnquiry”))

To run it you need mongoDB >= 4.2 , because i use pipeline inside the update.
Also because your driver might not support mongoBD>=4.2,run command is safe way,
and answer can be readable for all.If you driver supports mongoDB >=4.2 take the pipeline
and add it to your java update() method.

import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("YOURDATABASE");

Document buildInfoResults = database.runCommand(COMMAND);
System.out.println(buildInfoResults.toJson());
COMMAND(convert this json to org.bson.Document,and pass it as argument above)=

{
  "update": "YOUR_COLLECTION_NAME",
  "updates": [
    {
      "q": {},
      "u": [
        {
          "$addFields": {
            "channel": {
              "$cond": [
                {
                  "$eq": [
                    "$channel.name",
                    "switch"
                  ]
                },
                {
                  "_id": "$channel._id",
                  "name": "$channel.name",
                  "formats": {
                    "$map": {
                      "input": "$channel.formats",
                      "as": "format",
                      "in": {
                        "$cond": [
                          {
                            "$eq": [
                              "$$format.formatName",
                              "ISO8583-93"
                            ]
                          },
                          {
                            "_id": "$$format._id",
                            "formatName": "$$format.formatName",
                            "description": "$$format.description",
                            "fields": "$$format.fields",
                            "messages": {
                              "$map": {
                                "input": "$$format.messages",
                                "as": "formatMessage",
                                "in": {
                                  "$cond": [
                                    {
                                      "$eq": [
                                        "$$formatMessage.name",
                                        "balanceEnquiry"
                                      ]
                                    },
                                    {
                                      "_id": "$$formatMessage._id",
                                      "name": "$$formatMessage.name",
                                      "alias": "newAlias",
                                      "messageIdentification": "$$formatMessage.messageIdentification",
                                      "messageType": "$$formatMessage.messageType",
                                      "messageFormat": "$$formatMessage.messageFormat"
                                    },
                                    "$$formatMessage"
                                  ]
                                }
                              }
                            }
                          },
                          "$$format"
                        ]
                      }
                    }
                  }
                },
                "$channel"
              ]
            }
          }
        }
      ],
      "multi": true
    }
  ]
}

From this command you only care to edit the below part,i just updated the alias here,to “newAlias”
and didn’t added the fields and description fields,you can do any change you want.

{
 "_id": "$$formatMessage._id",
 "name": "$$formatMessage.name",
 "alias": "newAlias",
 "messageIdentification": "$$formatMessage.messageIdentification",
 "messageType": "$$formatMessage.messageType",
 "messageFormat": "$$formatMessage.messageFormat"
} 

The results i got was

{
  "_id": "Object ID ",
  "name": "switch",
  "formats": [
    {
      "_id": "Object ID ",
      "formatName": "ISO8583-93",
      "description": "ISO Format",
      "fields": [
        {
          "name": "0",
          "alias": "MTI",
          "lenght": "4",
          "description": "",
          "type": "FIXED",
          "dataType": "",
          "required": true
        }
      ],
      "messages": [
        {
          "_id": "Object ID ",
          "name": "balanceEnquiry",
          "alias": "newAlias",
          "messageIdentification": "",
          "messageType": "",
          "messageFormat": ""
        },
        {
          "_id": "Object ID ",
          "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": ""
            }
          ]
        }
      ]
    },
    {
      "_id": "Object ID ",
      "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
        }
      ]
    }
  ]
}

Hope that helps.