0

I have a query that finds the items I'm looking for:

db.user.find({email:/xhjvv123/}).pretty()

These are emails that all have that string (xhjvv123) as the first 8 characters. I want to find all emails with that string and then remove the string.

I think I can use $substr, but I can only find examples that return data. How to I actually change the string?

EDIT based on comments

ex. I have an email xhjvv123dbw@test.com. I want to change the email to just dbw@test.com. This will be true for hundreds of emails; i want to find all the emails with the string xhjvv123, remove that string, and leave the rest of the email intact.

styvane
  • 59,869
  • 19
  • 150
  • 156
DBWeinstein
  • 8,605
  • 31
  • 73
  • 118
  • Have you tried: http://docs.mongodb.org/manual/reference/method/db.collection.update/ and http://docs.mongodb.org/manual/reference/operator/update/set/ these should help you giving more insight in changing value – akshay Jun 03 '15 at 18:06
  • @akshay Thanks! But, don't those require that the new data all be the same? If I was replacing every email I found with the same string, I could use those. I'm just trying to remove the same part of each string, but the string left behind is different every time. – DBWeinstein Jun 03 '15 at 18:11
  • Possible duplicate http://stackoverflow.com/questions/12589792/how-to-replace-substring-in-mongodb-document – akshay Jun 03 '15 at 18:16
  • Do you want to update your documents? – styvane Jun 03 '15 at 18:21
  • @Michael Hopefully my edits have cleared up exactly what I'm trying to do. I don't want to use the `update` function in particular. I want to use whatever works. – DBWeinstein Jun 03 '15 at 18:25

3 Answers3

0

If your email stars with exact matching xhjvv123 this then use following query to remove :

db.collectionName.remove({"email":{"$regex":"^\\xhjvv123"}})

Or if email contains xhjvv123 this in any where then use above as

db.collectionName.remove({"email":{"$regex":"\\xhjvv123"}})

more about $regex.

EDIT

If you want to update matching email to other then use same in query and use your email in set as below :

db.collectionName.update({"email":{"$regex":"^\\xhjvv123"}},{"$set":{"email":"dbw@test.com"}},false,true)

or

db.collectionName.update({"email":{"$regex":"\\xhjvv123"}},{"$set":{"email":"dbw@test.com"}},false,true)

Or simply try this :

db.collectionName.update({"email":{"$regex":"xhjvv123"}},{"$set":{"email":"dbw@test.com"}},false,true)

Other option

If you run this on mongo shell

var regex = /xhjvv123/
var matchingEmail = "xhjvv123dbw@test.com"
var result = matchingEmail.replace(regex,"")
db.collectionName.update({"email":regex},{"$set":{"email":result}},false,true)

update

multi is set to true, the update() method updates all documents that meet the criteria.

So I don't think to iterate over all documents and replace string, multi:true do the job :)

Neo-coder
  • 7,715
  • 4
  • 33
  • 52
  • Thanks, but I'm not trying to remove the entire email. I just want to remove the part that I'm using to identify the email. So i'm trying to remove the part of the email that has the string. ex. I have an email `xhjvv123dbw@test.com`. I want to change the email to just `dbw@test.com`. – DBWeinstein Jun 03 '15 at 18:12
0

You can use the cursor.forEach method with the String.prototype.replace() method

var reg = /xhjvv123/;
db.user.find({ email: reg }).forEach(function(doc){
    doc.email = doc.email.replace(reg, "");
    db.user.save(doc);
})

Demo

> db.user.insert({'email': "xhjvv123dbw@test.com"})
WriteResult({ "nInserted" : 1 })
> var reg = /xhjvv123/;
> db.user.find({ email: reg }).forEach(function(doc){
...     doc.email = doc.email.replace(reg, "");
...     db.user.save(doc);
... })
> db.user.find()
{ "_id" : ObjectId("556f76e82db797abad412f56"), "email" : "dbw@test.com" }
> 
styvane
  • 59,869
  • 19
  • 150
  • 156
  • I tried this using the mongo shell and could not get it to work. i'm sure, however, that i'm doing something wrong. Do I need to put it in a script? – DBWeinstein Jun 03 '15 at 21:14
  • @dwstein what do you mean by *could not get to work*? added demo – styvane Jun 03 '15 at 21:50
0

please try below :

db.user.find({"email":/xhjvv123/}).ForEach( function(mydoc)
{
mydoc.email = mydoc.email.slice(9,this.email.length);
db.user.save(mydoc);
}
);

(or)

May be we you can try below as well in shell, not sure though :

db.user.update( {"email":/xhjvv123/}, {$set:{"email":{this.email.slice(9,this.email.length)}} , multi:true};
Yathish Manjunath
  • 1,919
  • 1
  • 13
  • 23