MongoDB User Guide

MongoDB is a storage-distributing file database program, and it is a product between a relational database and a non-relational database and the features in the MongoDB are the most abundant among in all non-relational databases and it is the most relational database-like. Data is structured very loosely in MongoDB, and so MongoDB can storage more complex data types. Most of features like table query are supported by MongoDB and you are allowed to create indexes for fields in MongoDB.

At present, versions supported by are MongoDB 2.4 and above. The ways of starting MongoDB 2.0 and MongoDB 3.0 in the authentication mode or the non- authentication mode are the same, but the ways of creating databases are different.

To use a MongoDB database, please start the MongoDB service to be connected first. Following contents illustrate how to use MongoDB by using mongodb- win32-i386-2.6.6.

Start the MongoDB service

  1. There is no authentication when you start a MongoDB service, and you can perform any operation to your database after login. If your mongod.exe is saved in “C:\MongoDB \bin”, the starting command is: mongod.exe --dbpath C:\MongoDB\data --logpath C:\MongoDB\log\log.txt
  2. If you add the authentication parameter after staring a service, it means enabling authorization. And so if you created a user in a database, the user only can perform actions on this database unless assigning to it roles in other databases. For an admin database, the user can perform any action on any database from MongoDB. mongod.exe --dbpath C:\MongoDB\data --logpath C:\MongoDB\log\log.txt -auth.
  3. If you want to add or delete a user, you can start MongoDB service in the non-authentication mode or the authentication mode and adding users to the admin database and authorizing them.
  4. When you started service, the error “Unclean shutdown detected” may be appeared because of you did not close the service normally, and you can repair and the re-start it. The command is: mongod.exe --repair --dbpath C:\MongoDB\data

The settings to start MongoDB in the authentication mode and the non- authentication mode are different. The detail differences are describes in following contents.

Create a database

  • When you start MongoDB service in the non-authentication mode, you need to enter a name for the database you want to create without filling anything into username, password, retype password. And if set up username and password, the database can not be created successfully.
  • When you start MongoDB service in the authentication mode, you need to enter an existed database name with correct username and password to connect the database successfully. The command of creating a database (like creating test database) is as follows:
  • Under the non-authentication mode, in the mongo.exe, execute:

use test

db.addUser("map","map")

  • Under the authentication mode: mongod.exe --dbpath C:\MongoDB\data --logpath C:\MongoDB\log\log.txt -auth
  • Starting MongoDB service and then user “map” can perform operations on test database.
  • Note : The codes above is the method creating a database in MongoDB 2.0, however, the way of creating database under the non-authentication mode in MongDB 3.0 is different. Take mongodb-win32-v3.4.0 as an example:
  • Under the non-authentication mode, in the mongo.exe, execute:

use admin

db.createUser({user:'admin',pwd:'admin',roles:[{role:"userAdminAnyDatabase", db:"admin"}]})

db.system.version.update({'_id':'authSchema'},{$set:{'currentVersion':3}})

db.system.version.find()

use test

db.createUser({user:'map',pwd:'map',roles:[{role:"dbOwner",db:"test"}]});

  • Under the authentication mode, re-start MongDB service: mongod.exe --setParameter authenticationMechanisms=MONGODB-CR --auth
  • test database is created and a user is created in text with both username and password are map.

And so you start MongoDB service in the authentication mode, the Database Name is test, and the User Name and Password are map in the connecting-database dialog box.

Open a database

The connection setting parameters to open a datasource are inconsistent when the service is started in user authentication and non-user authentication mode, as shown in the following table:

MongoDBDatabaseGuide