Skip to content

Instantly share code, notes, and snippets.

@nfhipona
Last active June 10, 2020 11:09
Show Gist options
  • Save nfhipona/52dcd410b15a677deb17a0be7b624beb to your computer and use it in GitHub Desktop.
Save nfhipona/52dcd410b15a677deb17a0be7b624beb to your computer and use it in GitHub Desktop.
MongoDB Dump File Command

Create dump mongodump

mongodump --host <DB_HOST> --port <DB_PORT> -u <DB_USER> -p <DB_PASS> -d <DB_NAME> --authenticationDatabase admin

Options

--quiet -- if system is running out of memory

--out -- output directory -- defaults to dump/<db_name>

Create DB dump -- with options

mongodump --host <DB_HOST> --port <DB_PORT> -u <DB_USER> -p <DB_PASS> -d <DB_NAME> --authenticationDatabase admin --quiet --out <FOLDER_NAME>

Load data from dump mongorestore

With Index Restore

mongorestore --host <DB_HOST> --port <DB_PORT> -u <DB_USER> -p <DB_PASS> -d <DB_NAME> foldername

Without Index Restore

mongorestore --noIndexRestore --host <DB_HOST> --port <DB_PORT> -u <DB_USER> -p <DB_PASS> -d <DB_NAME> foldername

Import Single Collection from CSV [1st Column will be treat as Col/Key Name]

mongoimport --host <DB_HOST> --port <DB_PORT> -u <DB_USER> -p <DB_PASS> -d <DB_NAME> --collection <COLLECTION_NAME> --type csv --headerline --file /path/to/myfile.csv

Import Single Collection from JSON

mongoimport --host <DB_HOST> --port <DB_PORT> -u <DB_USER> -p <DB_PASS> -d <DB_NAME> --collection <COLLECTION_NAME> --file input.json

Get mongo DB active port

mongo
use admin;
db.auth('<DB_USER>', '<DB_PASS>');
db.serverCmdLineOpts();

Sample Log serverCmdLineOpts()

{
	"argv" : [
		"/usr/bin/mongod",
		"--config",
		"/etc/mongod.conf"
	],
	"parsed" : {
		"config" : "/etc/mongod.conf",
		"net" : {
			"port" : 27017
		},
		"security" : {
			"authorization" : "enabled"
		},
		"storage" : {
			"dbPath" : "/var/lib/mongodb",
			"journal" : {
				"enabled" : true
			}
		},
		"systemLog" : {
			"destination" : "file",
			"logAppend" : true,
			"path" : "/var/log/mongodb/mongod.log"
		}
	},
	"ok" : 1
}
@nfhipona
Copy link
Author

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