MongoDB Export a collection (general)
MongoDB is a NoSQL database, that stores information in documents instead of tables, and it can store a lot of information.
It looks like that:
test> db.test.find()
[ { _id: ObjectId(’69edadcfb6b4cff75744ba89′), name: ‚Morgentest‘ } ]
You see it follows the JSON standard for documents. Ok, it stores this for human-readable information, but there is a special format that is called BSON that it uses, but this is not the subject of this blog entry.
If you have a full database (or an important database) for example my Spanish database where I have stored 1 000 entries, it should to have a backup if something goes wrong with the database or with the program for a good sleep in the night.
You can use this kind of command:
mongoexport --db=Espana --collection=ciudad --out=/home/xxx/Documents/espania_ciudad_backup_13072026_1000_Eintraege.json
mongoexport is the name of the program
- db = the database
- collection = your collection
- out = where you want to save the JSON file and the name of your file.
mongoexport comes with a program collection
mongodb-database-tools-100.17.0-1.x86_64
In my case it is a rpm file on Linux.
With this command without root permission you can check your installation, if you have installed, otherwise you can install it.
xxxx@linux ~]$ rpm -qa | grep mongo
By the way mongodb-compass is the graphical tool for the Shell (the command prompt of the Mongosh).