Mr. K.D.
Mr. K.D.

Reputation: 31

Replace string in one node of Mongodb document

I need your help to write one replace query. I have connected one third party software mongodb. I am SQL guy and not able to write query of replace specific node value.

I am using Robo 3T software. Below is one record json from the document.

{
    "_id" : ObjectId("6036c343445f3640c53e0a"), 
    "shares" : [ 
        {
            "shareId" : ObjectId("6035cc6acf345f3640c53d1a"),
            "type" : "user"
        }, 
        {
            "shareId" : ObjectId("6035b536cf345f3640c53cae"),
            "type" : "group",
            "rule" : "view",
            "subscribe" : false
        }
    ],
    "tags" : [],
    "lastUsed" : ISODate("2021-03-09T16:51:30.059Z"),
    "usageCount" : 1,
    "layout" : {
       too many values with multiple node.
    },
    "instanceType" : "owner",
    "original" : null,
    "editing" : true,
    "filters" : [ 
        {
            "jaql" : {
                "table" : "Xyz",
                "column" : "title",
                **"dim" : "[dbo.ketan.Title]",**
                }
            }
            "disabled" : false
        }, 
        {
            "jaql" : {
                "table" : "abc",
                "column" : "name",
                **"dim" : "[dbo.ketan.name]",**
                "datatype" : "text",
                "merged" : true,
                "title" : "Inventor Name",
                "filter" : {
                    "explicit" : false,
                    "multiSelection" : true,
                    "all" : true
                },
                "collapsed" : false
            },
            "instanceid" : "1B1E4-F6DC-28",
            "isCascading" : false
        }, 
        
}

Here I want to replace "dim" : "[dbo.ketan.name]", to "dim" : "[kiran.name]",

Can you please help me to write query ?

This is my first post. The Writing may be not up to mark. Please let me know if you need any more details.

Thank you.

Upvotes: 2

Views: 219

Answers (1)

Bilal Bin Zia
Bilal Bin Zia

Reputation: 694

This is how you can acheive the functionality. Although your MongoDB Version should be >= 3.6

db.collection.update(                                     //collection
  {"filters.jaql.dim": {"$regex": "dbo.ketan"}},          //update filter
  {"$set": {"filters.$[elem].jaql.dim": "[kiran.name]"}}, //update action
  {
    "arrayFilters": [
      {
        "elem.jaql.dim": {
          "$regex": "dbo.ketan"
        }
      }],
    "multi": true
  }                                               //filters for all element to be updated
)

To see this logic in action: MongoPlayground Example

Upvotes: 1

Related Questions