I have a field of phone numbers where a random variety of separators have been used, such as:
932-555-1515
951.555.1255
(952) 555-1414
I would like to go through each field that already exists and remove the non numeric characters.
Is that possible?
Whether or not it gets stored as an integer or as a string of numbers, I don't care either way. It will only be used for display purposes.
You'll have to iterate over all your docs in code and use a regex replace to clean up the strings.
Here's how you'd do it in the mongo shell for a
test
collection with aphone
field that needs to be cleaned up.Starting in
Mongo 4.4
, the$function
aggregation operator allows applying a custom javascript function to implement behaviour not supported by the MongoDB Query Language.And coupled with improvements made to
db.collection.update()
inMongo 4.2
that can accept an aggregation pipeline, allowing the update of a field based on its own value,We can manipulate and update a field in ways the language doesn't easily permit and avoid an inefficient find/foreach pattern:
The update consist of:
a match query
{ "x": { $regex: /[^0-9]/g } }
, filtering documents to update (in our case any document that contains non-numeric characters in the field we're interested on updating).an update aggreation pipeline
[ { $set: { active: { $eq: [ "$a", "Hello" ] } } } ]
(note the squared brackets signifying the use of an aggregation pipeline).$set
is a new aggregation operator and an alias for$addFields
.$function
takes 3 parameters:body
, which is the function to apply, whose parameter is the string to modify. The function here simply consists in replacing characters matching the regex with empty characters.args
, which contains the fields from the record that thebody
function takes as parameter. In our case,"$x"
.lang
, which is the language in which thebody
function is written. Onlyjs
is currently available.Based on the previous example by @JohnnyHK, I added regex also to the find query: