![]() However, mongorestore performs inserts only and does not perform updates. Mongorestore can create a new database or add data to an existing database. This means that it will not overwrite a document in the database that already exists: One main difference between mongorestore and mongoimport is that mongorestore is insert only. The mongorestore program loads data from either a binary database dump created by mongodump The compliment to mongodump is thee mongorestore utility to restore a mongo database mongodump -db=mydb -username=myuser -password=somepass -authenticationDatabase=admin -archive=mydb.archive Mongorestore The -archive option creates one file for us which we can use to reimport the database with mongorestore. Note, you cannot use the -archive flag and -out flags together in the same mongodump command. The -archive flag allows us to specify the name of the archive file. This is useful if you are transferring files between hosts or sending backup files between servers. We can also run the database backup to an archive file, instead of dumping all of the files into a dump directory. We should now see inside our databse/mydb folder the and order.bson added. For instance, let’s add the order collection to our out dump folder: mongodump -user=myuser -db=mydb -password=somepass -authenticationDatabase=admin -out=databases -collection=order Doing so will not overwrite the contents of the output folder and you can sequentially dump collections. We can run this command as many times as we’d like, specifying one collection at a time to dump to backup. This will now create a folder structure such that. Let’s say we wanted to only dump our product collection we could run: mongodump -user=myuser -db=mydb -password=somepass -authenticationDatabase=admin -out=databases -collection=product If for instance, you only want to back up one collection you can specify the -collection flag with the name of the collection you would like to dump. For instance, if we want to call our dump directory databases instead of dump we could use: mongodump -user=myuser -db=mydb -password=somepass -authenticationDatabase=admin -out=databasesīy default mongodump dumps all of the collections into our output folder and within a folder that reflects the name of the database. For instance, if you want to change the name of the dump directory you can use the -out flag to specify the name of the directory you want the database dumped to. In mongodump we can change the behavior of the output. bson will hold the documents (data) for each collection. metadata.json files will hold metadata information such as options, indexes and ns (for the collection namespace) for each of the collection. We will have a BSON file and a JSON file for each collection. Inside the database specific folder we will have two files for each collection in our database. In our case, there would be a sub directory inside called mydb so your new folder structure would look like. Inside you will have a subfolder with the name of the database. If it’s the same, then you would change the -authenticationDatabase flag to the correct database or in URI format the authSource querystring option to the correct database.Įither of these methods dumps the whole database into a dump directory (named dump by default), in the current working directory you ran the command in. This is assuming that the authentication db (which stores your database user) is different than the database that you would like to dump. ![]() Or with a Mongo URI format: mongodump -uri="mongodb://myuser: :27107/mydb?ssl=false&authSource=admin To export a database called mydb from your localhost with mongoexport you can use: mongodump -user=myuser -db=mydb -password=somepass -authenticationDatabase=admin You can connect to your mongo database using either: -uri with a correct formatted uri string OR with the -user, -db and -password flag options but you cannot use a combination of both. ![]() ![]() We can look at a few of the command line options for the mongodump utility and walk through examples of how to use it. Using mongodump you can later restore your database with the mongorestore utility. Say for instance, you are scheduling backups of your database on a daily basis using a cron job method. Mongodump is typically used for a backup strategy. can export data from standalone, replica set, and sharded cluster deployments. mongodump can export data from either mongod or mongos instances i.e. Mongodump is a utility for creating a binary export of the contents of a database. ![]() Here we will discuss how to use both methods and when one method might work better than the other. If you have worked with MongoDB you may know that there are two different ways to export and import a Mongo database: mongodump/mongorestore and mongoexport/mongoimport. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |