如果您的電子郵件恆星精確匹配xhjvv123
這個然後用下面的查詢刪除:
db.collectionName.remove({"email":{"$regex":"^\\xhjvv123"}})
或者如果郵件中包含xhjvv123
這在任何地方,然後用上面
db.collectionName.remove({"email":{"$regex":"\\xhjvv123"}})
更多$regex 。
編輯
如果您想更新匹配郵件給其他然後在query
使用相同,使用您的電子郵件中set
如下:
db.collectionName.update({"email":{"$regex":"^\\xhjvv123"}},{"$set":{"email":"[email protected]"}},false,true)
或
db.collectionName.update({"email":{"$regex":"\\xhjvv123"}},{"$set":{"email":"[email protected]"}},false,true)
或者只需試試這個:
db.collectionName.update({"email":{"$regex":"xhjvv123"}},{"$set":{"email":"[email protected]"}},false,true)
其他選項
如果在蒙戈運行這個殼
var regex = /xhjvv123/
var matchingEmail = "[email protected]"
var result = matchingEmail.replace(regex,"")
db.collectionName.update({"email":regex},{"$set":{"email":result}},false,true)
update
多設置爲true,update()方法更新符合條件的所有文件。
所以我不認爲遍歷所有文件並替換字符串,多:真做的工作:)
您是否嘗試過:http://docs.mongodb.org/manual/reference/method/db.collection.update/和http://docs.mongodb.org/manual/reference/operator/update/set/這些應該可以幫助你在改變價值方面給予更多的洞察力 – akshay
@akshay謝謝!但是,那些要求新數據都一樣嗎?如果我用相同的字符串替換每封發現的電子郵件,我可以使用這些字符串。我只是試圖刪除每個字符串的相同部分,但每次都會留下不同的字符串。 – dwstein
可能的重複http://stackoverflow.com/questions/12589792/how-to-replace-substring-in-mongodb-document – akshay