MongoDB Commands
Dr. Archana B Saxena,
Professor, Department of Information Technology,
[Link]@[Link], archanabsaxena@[Link]
NoSQL and RDBMS Mapping
• Table ->>>>>>>>>>>>Collection
• Rows->>>>>>>>>>>>Documents
• Columns->>>>>>>>>>>>Fields
• Start mongo database : open command prompt and type “mongod”
• Start mongo shell: open command prompt and type “mongo”
• Type commands in mongo shell.
CRUD operations
The basic methods of interacting with a MongoDB server are called
CRUD operations. CRUD stands for Create, Read, Update, and Delete.
These CRUD methods are the primary ways you will manage the data in
your databases.
• C-Create
• R-Read
• U-Update
• D-Delete
Database commands
Command Explanation Example
Mongodb stores records as documents[JSON format] which are gathered together as collection.
A single database can contain collection.
show dbs; To see all existing databases Show dbs
Use <name of database> The use Use mylib
Database_name command
makes a new database in the
system if it does not exist, if
the database exists it uses that
database:
New database will not be visible if there is no data in the database
db To know your selected db
database
[Link]() To drop current database [Link]();
Collection Commands
Command Explanation Example
Show collections; To see the collections of a database Show collections;
[Link]()
[Link]()
[Link](“name”) To create a database in a collection [Link](“lib”)
Collection can directly be created at the time of creating a record/document
[Link]({“key”:”value” Create collection and insert document [Link]({“bookname”:”my
}) in it one command mongodb book”})
db.<name of collection>.find() To see all the documents of a [Link]();
collections
[Link]({selection To update a document in a collection [Link]({‘Name’:’Bill
criteria, updated data}) Vanners’}, {$set:{‘Name’: ‘Inside
JVM’}})
[Link]() To drop a collection from database [Link]();
[Link](“criteria”) Remove a document from collection [Link]({‘Name’:’ABC’})
Accessing MongoDB from [Link] (Data
Retrieval)
• Mongoose : Mongodb Object Modeling for [Link]
• Install package mongoose in your project:
• Command: npm I mongoose
Environment variable
• To run the mongod [for server] or mongo [for client] from anywhere u
need to set the environment variable.
• Control Panel ->system and security -> system -> advance system settings -
>environment variable ->
• Set path variables and add address of c:/programfiles/mongoDB/bin path
here
MongoDB server can be run as service
• To run the MongoDB server as a service, you need to select this
option during installation.
• Set the path variable so the MongoDB server and shell can be run
from anywhere on your machine.
• After path setting, restart the machine to run mongodb server as
background service.