Skip to content

Instantly share code, notes, and snippets.

@MahbbRah
Created July 15, 2022 06:38
Show Gist options
  • Save MahbbRah/fb604c324ababe0cb785c833dea69fee to your computer and use it in GitHub Desktop.
Save MahbbRah/fb604c324ababe0cb785c833dea69fee to your computer and use it in GitHub Desktop.
MongoDB helpful commands; and export/imports commands as well.

We'll list down here some helpful MongoDB commands these are helpful and requires in day job!

Export mongoDB collections from mongo Cloud via mongo cli

Sometimes it requires to dump all the collections or specified one's I'm gonna mention here how you can get backup (dump) file from MongoDB for each collections Let's say you want to take a backup for users colleciton for that the command will be like below mongoexport --uri="mongodb+srv://[username]:[password]@cluster0.dtwwj.mongodb.net/[database_name]" --collection=users --out=users.json The command above self descriptive, on --collection param we're providing our target collection name then on --out we're simply naming the file that would should be exported as.

Import MongoDB Collections From json exported file

to import mongoDB collections from json file we simply use the mongoimport utility provides with mongo cli the format should be something like this (we'll use MongoDB cloud in this example) mongoimport --uri "mongodb+srv://[username]:[password]@cluster0.dtwwj.mongodb.net/[database_name]" users.json where users.json is the dump file from your backup, we're just restoring the backup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment