Create MongoDB database and account user

MongoDB Connection

Check db server:

$ service mongod status

Run db server:

$ sudo service mongod start

$ service mongod status
   Active: active (running)

Getting Started with the mongo Shell

Start the mongo Shell:

$ mongo

// To display the database you are using:
$ MongoDB> db

// Print help
$ MongoDB> help

// Exit mongo shell
$ MongoDB> exit 
$                    help on db methods             help on collection methods                    sharding helpers                    replica set helpers
help admin                   administrative help
help connect                 connecting to a db help
help keys                    key shortcuts
help misc                    misc things to know
help mr                      mapreduce

show dbs                     show database names
show collections             show collections in current database
show users                   show users in current database
show profile                 show most recent system.profile entries with time >= 1ms
show logs                    show the accessible logger names
show log [name]              prints out the last segment of log in memory, 'global' is > default
use <db_name>                set current database                list objects in collection foo { a : 1 } )     list objects in foo where a == 1
it                           result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x   set default number of items to display on shell
exit                         quit the mongo shell
MongoDB Enterprise >

To switch databases, issue the use <db>

How to create a MongoDB database

With MongoDB you create a database the first time you save data into it.

$ mongo

mongo> show dbs

admin         0.000GB
local         0.000GB

Step 1. “Use” the database you wish to create:

rather than explicitly creating a database, MongoDB will create a database when it first has data saved to.

In order to save data, we need to connect to our new database even though it doesn’t exist yet.

// new database: `ts-mean-test`
mongo> use ts-mean-test

the database hasn’t been created yet.

Step 2. Insert some data into the ts-mean-test database:

there are no tables, rows or columns.
In their place, MongoDB uses collections and objects. Think of collections as tables, and objects as table rows.

mongo> db

// new collection: `teams`
> show dbs

admin     0.000GB
local     0.000GB
ts-mean-test  0.000GB

That will create a new collection called “teams”, and save an object with two fields “Country” and “GroupName”. Crucially, this will now also save our database.
Check that it has been created by running show dbs again.

Step 3. testing by reading the data back out:

mongo> show collections

> db.teams.find()

{ "_id" : ObjectId("5902ef7eeeffb94cf18735d4"), "Country" : "Korea", "GroupName" : "Codebits.Design" }
  • A database holds a set of collections
  • A collection holds a set of documents
  • A document is a set of fields
  • A field is a key-value pair
  • A key is a name (string)
  • A value is a – basic type like string, integer, float, timestamp, binary, etc., a document, or an array of values

How to create a MongoDB accout user

User Management on MongoDB

  • show users: Print a list of users for current database.
  • db.createUser(user, writeConcern) : Creates a new user.

Create User with Roles :

The following operation creates mongoUser in the ts-mean-test database and gives the user the readWrite and dbAdmin roles.

mongo> use ts-mean-test
> db.createUser(
     user: "mongoUser",
     pwd: "password",
     roles: [ "readWrite", "dbAdmin" ]


Show the account users of ts-mean-test database:

mongo> use ts-mean-test
> show users

        "_id" : "ts-mean-test.mongoUser",
        "user" : "mongoUser",
        "db" : "ts-mean-test",
        "roles" : [
                        "role" : "readWrite",
                        "db" : "ts-mean-test"
                        "role" : "dbAdmin",
                        "db" : "ts-mean-test"

> exit

Connect ts-mean-test database with mongoUser account:

$ mongo ts-mean-test -u mongoUser -p
Enter password:

mongo> db  


Leave a Reply