MongoDB – Tutorial, main commands
The goal of that article is to collect in one place the most common commands for working with NoSQL database MongoDB with working examples of it.
Suppose you have installed and run the MongoDB server. If not, you can easily install it from the official MongoDB website. In order to start working with it run the following command:
mongo
-----------------------------------------------------------------------
ubuntu@ip-172-31-31-139:~$ mongo
MongoDB shell version: 2.6.10
connecting to: test
>
-----------------------------------------------------------------------
That means that you are in Mongo console now.
In order to view a list of available databases run:
show dbs
Output:
-----------------------------------------------------------------------
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
>
-----------------------------------------------------------------------
To select a base for manipulations or create new you can use following command:
use
-----------------------------------------------------------------------
> use test
switched to db test
>
-----------------------------------------------------------------------
To find out which database is selected run:
db
-----------------------------------------------------------------------
> db
test
>
-----------------------------------------------------------------------
If you want to delete your DataBase:
dropDatabase
-----------------------------------------------------------------------
> db.dropUser("testUser")
true
>
-----------------------------------------------------------------------
For the next step you can set a database with which you are going to work, but you will need a user with access rights to it!
To view the list of users run:
getUsers
-----------------------------------------------------------------------
> db.getUsers()
[
{
"_id" : "test.testUser",
"user" : "testUser",
"db" : "test",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
}
]
},
{
"_id" : "test.testUser2",
"user" : "testUser2",
"db" : "test",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
}
]
}
]
>
-----------------------------------------------------------------------
In order to create a user:
createUser
-----------------------------------------------------------------------
> db.createUser({ user: 'testUser', pwd: 'testPass', roles: [{ role: 'readWrite', db:'test'}]})
Successfully added user: {
"user" : "testUser",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
}
]
}
> db.createUser({ user: 'testUser2', pwd: 'testPass', roles: [{ role: 'readWrite', db:'test'}]})
Successfully added user: {
"user" : "testUser2",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
}
]
}
>
-----------------------------------------------------------------------
If you want to delete user:
dropUser
-----------------------------------------------------------------------
> db.dropUser("testUser")
true
>
-----------------------------------------------------------------------
Awesome, now when you have a database and a user - it's time to improve security.
As for most databases, the Mongo has its own security and access settings in the mongod.conf file, in which you need to set the network settings:
-----------------------------------------------------------------------
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
-----------------------------------------------------------------------
As well as security settings:
-----------------------------------------------------------------------
#security
security:
authorization: 'enabled'
-----------------------------------------------------------------------
Also, to connect to your database remotely via the console, you need to install the MongoDB locally and add a path to the environment variables or call commands using the full address.
For Windows, for example, the address will look like that:
C:\Program Files\MongoDB\Server\4.2\bin\
And a command to connect to remote MongoDB:
.\mongo --host IP:PORT
-----------------------------------------------------------------------
PS C:\Program Files\MongoDB\Server\4.2\bin> .\mongo.exe --username testUser --password testPass --authenticationDatabase test --host 34.245.11.182:27017
MongoDB shell version v4.2.1
connecting to: mongodb://34.245.11.182:27017/?authSource=test&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("2a0de2a2-a6ca-4149-a3c5-b3125c65a69f") }
MongoDB server version: 3.6.16
WARNING: shell and server versions do not match
>
-----------------------------------------------------------------------
To creating a dump in the local folder C:\dump\ run:
.\mongodump --host IP:PORT --out=/dump/
-----------------------------------------------------------------------
PS C:\Program Files\MongoDB\Server\4.2\bin> .\mongodump.exe --username backupUser --password testPass --authenticationDatabase admin --host 34.245.11.182:27017 --out=/dump/
2019-12-18T17:09:32.019+0300 writing admin.system.users to
2019-12-18T17:09:32.163+0300 done dumping admin.system.users (3 documents)
2019-12-18T17:09:32.164+0300 writing admin.system.version to
2019-12-18T17:09:32.363+0300 done dumping admin.system.version (2 documents)
2019-12-18T17:09:32.364+0300 writing test.customers to
2019-12-18T17:09:32.499+0300 done dumping test.customers (1 document)
PS C:\Program Files\MongoDB\Server\4.2\bin>
-----------------------------------------------------------------------
If you want to restore your database from a local folder just run:
.\mongorestore --host IP:PORT dump/
-----------------------------------------------------------------------
PS C:\Program Files\MongoDB\Server\4.2\bin> .\mongorestore.exe --host 34.245.11.182:27017 C:/dump/
2019-12-18T17:57:46.499+0300 preparing collections to restore from
2019-12-18T17:57:46.677+0300 reading metadata for test.customers from C:\dump\test\customers.metadata.json
2019-12-18T17:57:46.756+0300 restoring test.customers from C:\dump\test\customers.bson
2019-12-18T17:57:46.836+0300 no indexes to restore
2019-12-18T17:57:46.836+0300 finished restoring test.customers (1 document, 0 failures)
2019-12-18T17:57:46.837+0300 1 document(s) restored successfully. 0 document(s) failed to restore.
PS C:\Program Files\MongoDB\Server\4.2\bin>
-----------------------------------------------------------------------
With all the work above you can easily perform basic manipulations with Mongo.