Full Stack Unit 3 - Part1
Full Stack Unit 3 - Part1
Installing MongoDB
Starting MongoDB
Stopping MongoDB
Administering User Accounts
Creating User Accounts
Listing Users
Removing Users
Configuring Access Control
Creating a User Administrator Account
Turning on Authentication
Creating a Database Administrator Account
Administering Databases
Displaying a List of Databases
Changing the Current Database
Creating Databases
Deleting Databases
Copying Databases
Managing Collections
Displaying a List of Collections in a Database
Creating Collections
Deleting Collections
Finding Documents in a Collection
Adding Documents to a Collection
Deleting Documents in a Collection
Updating Documents in a Collection
Connecting to MongoDB from Node.js
Q1: Building the MongoDB Environment
Description Steps
• To integrate MongoDB into your Node.js environment, the initial step is to install
the MongoDB server.
• MongoDB offers versions for various platforms, including Linux, Windows, Solaris,
and OS X, with an enterprise version.
• Enterprise version is subscription-based, offering enhanced security, management,
and integration support.
• To install MongoDB:
Installing Visit the MongoDB website and download the standard edition.
MongoDB (http://docs.mongodb.org/manual/installation/)
During installation:
o Download and extract MongoDB files.
o Add <mongo_install_location>/bin to your system path.
o Create a data files directory at
<mongo_data_location>/data/db
Start MongoDB using the console prompt with the command:
mongod –dbpath <mongo_data_location>/data/db
After installing MongoDB, managing the database engine involves starting and
stopping it.
To initiate the MongoDB database engine, execute the following command mongod
For example, to start MongoDB with specific port and dbpath parameters, you can use a
command like:
mongod –port 28008 –dbpath <mongo_data_location>/data/db
To halt the MongoDB database engine, ensuring a clean shutdown and termination
Stopping of current operations from the shell client:
MongoDB use admin // Switch to the admin db
db.shutdownServer() // Shut down the db engine
---------------------
Q2: Administering User Accounts
Once MongoDB is operational, the next step is to add users for database access.
MongoDB offers functionalities within the MongoDB shell to add, remove, and configure users.
These actions facilitate user management, ensuring controlled and secure access to the database.
Description Steps
Create new user accounts to control access to databases.
Use the db.createUser() command to add a new user.
Specify the username, password, and roles (permissions) for the new user.
Fields used when creating users with the db.createUser() method.
Field Format Description
user String Specifies a unique username.
pwd hash or string Specifies a user password
roles Array Specifies an array of user roles
Database roles that can be assigned to user accounts
Creating Role Description
User dbAdmin Allows the user to read from and write to the database
Accounts dbAdminAnyDatabase Allows the user to read from and write for all the database
userAdmin Allows the user to create and modify user accounts on the database
userAdminAnyDatabase Allows the user to create and modify user accounts for all the database
read Allows the user to read data from any collection within the database.
readWrite Allows the user to write to any collection within the database
readAnyDatabase Allows the user to read data from any collection for all the database
readWriteAnyDatabase Allows the user to write to any collection for all the database
Example :
use testDB // Switch to testDB
db.createUser( { user: "testUser", // Create Users with fileds & roles
pwd: "test",
roles: [ "readWrite", "dbAdmin" ] } )
User accounts in MongoDB are stored in the db.system.users collection for each
database.
The User object includes fields such as _id, user, pwd, roles, and other DBRoles.
To retrieve a list of user objects, one approach is to switch to the desired database
and execute the show users command.
For instance, switching to the admin database and listing users is accomplished with
Listing the following commands:
Users use admin // Switch to the admin db
show users // Listing users on the admin db
The following code gets a cursor for users in the admin database and returns the
count of users:
use admin
cur = db.system.users.find()
cur.count()
To remove users from MongoDB, the removeUser(<username>) method is used.
Before executing this method, it's necessary to switch to the database where the user
exists.
Removing
For instance, to remove the user "testUser" from the "testDB" database, the following
Users
commands are used in the MongoDB shell:
use testDB // Switch to testDB
db.removeUser("testUser") // Remove testUser from testDB