1

I have "Project" collection as below. I would like to update email domain in "users" & "owners" to "@mailm365.com"

{
"_id" : ObjectId("637e0af92ce49a1a816807bc"),
"title" : "Project-1",
"description" : "Project-1 Description",
"users" : [ 
    {
        "_id" : ObjectId("5f8881823542b1b499d7b351"),
        "fullname" : "User-1",
        "email" : "[email protected]"
    }
],
"owners" : [ 
    {
        "_id" : ObjectId("5f8881823542b1b499d7b351"),
        "fullname" : "User-2",
        "email" : "[email protected]"
    }, 
    {
        "_id" : ObjectId("61fb71ca294d34537dafe4c9"),
        "fullname" : "User-3",
        "email" : "[email protected]"
    }
]}

I have tried with the following query but no luck. Any help to make this update query work?

db.Project.find().forEach(function(project)
{
  project.users.forEach(function(user){
    var updatedDomain = user.email.replace('@mail.com', '@mailm365.com');
    user.email = updatedDomain;
  });

  project.owners.forEach(function(owner){
    var updatedDomain = owner.email.replace('@mail.com', '@mailm365.com');
    owner.email = updatedDomain;
  });
  
  db.Project.updateOne(
    {
      _id:project._id
    },
    {
       $set:
            {
                "users": project.users,
                "owners": project.owners
            }
    }   
    );
});

1 Answer 1

2

Query

  • you need pipeline update because you want to update based on previous value, and you also need $split function that is pipeline operator
  • 2 times the same code
  • $map on the array, $this will be the document member, and $mergeObjects with the new email
  • new email is split the old, take the first part before the @ and concat it with the @mailm365.com new docmain name

*this does it for all users,owners if you want to do it only for some, you can add a $cond inside the map to do it only in specific

PLaymongo

update(
{"_id": {"$eq": ObjectId("637e0af92ce49a1a816807bc")}},
[{"$set": 
   {"users": 
     {"$map": 
       {"input": "$users",
        "in": 
         {"$mergeObjects": 
           ["$$this",
             {"email": 
               {"$concat": 
                 [{"$first": {"$split": ["$$this.email", "@"]}},
                  "@mailm365.com"]}}]}}}}},
 {"$set": 
   {"owners": 
     {"$map": 
       {"input": "$owners",
        "in": 
         {"$mergeObjects": 
           ["$$this",
             {"email": 
               {"$concat": 
                 [{"$first": {"$split": ["$$this.email", "@"]}},
                  "@mailm365.com"]}}]}}}}}],
{"multi": true})
Sign up to request clarification or add additional context in comments.

2 Comments

We're on the same wavelength today. Here's a mongoplayground.net with virtually the same thing.
@rickhg12hs i stole your answer+posted first :P, i wrote the squery, see playmongo link, you might like it, its smaller

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.