skip to Main Content

I have below Mongo Collection

{
   "name":"test",
   "work":"BA",
   "contacts":[
      {
         "company":"xyz",
         "email":"http://www.google.com/check/com/2"
      },
      {
         "company":"xyz1",
         "email":"http://www.google.com/verify/com/4"
      }
   ]
}

I want to replace www.google.com from contacts email to www.test.com, Not complete URL only a particular string from email.
Any help appreciated!!

2

Answers


  1. Query1

    • $indexOfCP MongoDB >=3.6

    PlayMongo

    aggregate(
    [{"$set": {"m": "www.google.com", "r": "www.test.com"}},
     {"$set": 
       {"contacts": 
         {"$map": 
           {"input": "$contacts",
            "in": 
             {"$mergeObjects": 
               ["$$this",
                 {"email": 
                   {"$let": 
                     {"vars": {"index": {"$indexOfCP": ["$$this.email", "$m"]}},
                      "in": 
                       {"$cond": 
                         [{"$eq": ["$$index", -1]}, "$$this.email",
                           {"$concat": 
                             [{"$substrCP": 
                                 ["$$this.email", 0, {"$subtract": ["$$index", 0]}]},
                              "$r",
                               {"$substrCP": 
                                 ["$$this.email",
                                   {"$add": ["$$index", {"$strLenCP": "$m"}]},
                                   {"$subtract": 
                                     [{"$strLenCP": "$$this.email"},
                                       {"$add": 
                                         ["$$index", {"$strLenCP": "$m"}]}]}]}]}]}}}}]}}}}},
     {"$unset": ["m", "r"]}])
    

    Query2

    • $replaceAll MongoDB >= 4.4

    PlayMongo

    aggregate(
    [{"$set": 
       {"contacts": 
         {"$map": 
           {"input": "$contacts",
            "in": 
             {"$mergeObjects": 
               ["$$this",
                 {"email": 
                   {"$replaceAll": 
                     {"input": "$$this.email",
                      "find": "www.google.com",
                      "replacement": "www.test.com"}}}]}}}}}])
    
    Login or Signup to reply.
  2. 4.4+ :

    db.collection.update({
     "contacts.email": {
      $regex: "google"
     }
     },
    [
     {
       $addFields: {
         contacts: {
           $map: {
             input: "$contacts",
             as: "c",
             in: {
               $mergeObjects: [
                {
                  email: {
                  $replaceOne: {
                    input: "$$c.email",
                    find: "www.google.com",
                    replacement: "www.test.com"
                  },    
                 }
                },
                {
                   company: "$$c.company"
                }
               ]
              }
             }
            }
           }
          }
         ])
    

    Explained:

    Update via aggregation pipeline using replaceOne via $map/mergeObjects

    playground

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search