Skip to main content

Posts

MongoDB Backup and Restore

It's a general need in MongoDB development, to take backups or restore DB with an old backup. DB backup can be done at both DB level and individual Collection level. Let's see how we can perform different backup/restore operations.. DB Backup Complete DB Assuming database to backup is "mydb", the best thing is to back it as gzip to save some space on your Server. mongodump -- archive = mydb . 2017 - 09 - 21.gz -- gzip -- db mydb This will create the archive in the directory where you're executing the command. Another way is to take backup as bson documents. mongodump -h localhost -p 27017 -d mydb -o C:\mongobackup\20170921 A directory with db name will be created under  C:\mongobackup\20170921 In case you have multiple instances running on same server, you can use --host and --port options. Individual Collection mongodump -- collection myCollection -- db mydb This will create backup with same name as that of collection (myCollection in this
Recent posts

Creating MongoDB Service on Windows

There are different ways you can install MongoDB as Windows Service.. Using mongod mongod -- config "M:\mongodb\conf\mongod.cfg" -- serviceName "MongoDB" -- serviceDisplayName "MongoDB" -- serviceDescription "MongoDB Server Instance" -- install mongod -- logpath M : \mongodb\data\log\mongo . log -- dbpath M : \mongodb\data\db -- auth -- directoryperdb -- install Specifying argument directoryperdb creates separate directory structure for different DBs like admin, mydb etc. Using Windows Service command sc . exe create MongoDB binPath = "\"M:\mongodb\bin\mongod.exe\" --service --config=\"M:\mongodb\mongod.cfg\"" DisplayName = "MongoDB" start = "auto" Following the Windows service command, make sure Service console is not opened. Start/Stop MongoDB Service net start MongoDB net stop MongoDB Deleting MongoDB Service mongod --remove sc.exe delete MongoDB

How to create users in MongoDB

Assuming you've already completed the basic setup process mentioned in this blog . In this example, we'll see how we can create admin user and enable authorization. Connect to admin database On a cmd window, connect to MongoDB by hitting command " mongo ". By default, it connects to " test " database. Once connection is successful, switch to admin db #mongo #use admin Create Role After switching to admin db, create executiveFunction role db . runCommand ({ createRole : "executeFunctions" , privileges : [ { resource : { anyResource : true }, actions : [ "anyAction" ] } ], roles : [], writeConcern : { w : "majority" , wtimeout : 5000 } }); Create admin user In this step, we'll create admin user mongoadmin with password mongo123 db . runCommand ( { "createUser" : "mongoadmin" , "pwd" : "mongo123&qu