Mongo Find and Replace
This is a handy little utility for searching through all of the fields in all of your MongoDB documents and updating the values of those fields.
Replacing something in a specific field in all documents is easy enough with MongoDB, but what if you have a lot of fields across multiple documents in your MongoDB database that have the same error? What if you don't even know which fields in your documents contain the error? What if you need to update multiple documents across multiple collections?
And what kind of errors are we talking about? Some extra characters here, a frequently occurring misspelling, a wrong number? A
'true' that's really supposed to be a
true? A state abbreviation that needs to be expanded into its full version, or vice versa?
If this describes you, then you've come to the right spot.
npm install mongo-find-repace --save
Make a backup of your data before using this tool. It will do exactly what you tell it to every field in every document, so make sure you've got your regular expressions written in such a way that you don't accidentally change something you didn't intend to change.
From the MongoDB CLI (docs here):
- Import mongo-find-replace into a JS file:
const MongoFindReplace = ;
- Configure mongo-find-replace. The three required config options are the url to your MongoDB database, the database name, and the collection(s) containing the documents you want to search through and edit. See below for more advanced config (e.g. auth, etc.).
const MongoFR =dbUrl: 'mongodb://localhost:27017'dbName: 'example_db'collections: 'example_collection' // or for multiple collections: ['coll1, coll2', ...];
- Call the
#findtakes a regular expression,
#andReplaceWithtakes the string, number, or boolean that you'd like to replace your regular expression match with:
- Once you've got steps 1-3 done, save your file (e.g.
script.js), start up MongoDB, backup your data, head into your command line/terminal application, cd into the directory containing
script.js, and enter:
The script will then run, you should see console output indicating which step the script is on.
If you want to replace instances of 'colour' with 'color' in all fields in all documents in a given collection:
const mongo-find-replace = ;const MongoFR =dbUrl: 'mongodb://localhost:27017'dbName: 'example_db'collections: 'example_collection' // or for multiple collections: ['coll1, coll2', ...];MongoFR;
If you want to replace tabs with two spaces:
// config goes hereMongoFR;
If you want to eliminate something entirely from all fields in a document:
// config goes hereMongoFR;
Regexr is an excellent tool for both learning and testing regular expressions if you don't have a lot of experience with them.
Please open an issue!
- Open a new issue explaining the features you'd like to add or bugs you'd like to fix, or comment on an existing issue and express your desire to make a PR against it.
- Fork the repo
- If you don't have it already, install commitizen and use it for your commits (
- Create a new branch for your PR (e.g.
git checkout -b feature-add-nyan-cat'or
git checkout -b 'bug-fix-unit-tests')
- Make your PR
New contributors welcome! Just let me know if you have any questions.