You are on page 1of 9

MongoDB Commands

Dr. Archana B Saxena,


Professor, Department of Information Technology,
Archanab.saxena@jimsindia.org, archanabsaxena@gmail.com
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
Db.dropDatabase() To drop current database Db.dropDatabase();
Collection Commands
Command Explanation Example
Show collections; To see the collections of a database Show collections;
Db.getCollectionNames()
Db.getCollectionInfos()
db.createCollection(“name”) To create a database in a collection db.createCollection(“lib”)
Collection can directly be created at the time of creating a record/document
Db.collectionname.insert({“key”:”value” Create collection and insert document Db.lib.insert({“bookname”:”my
}) in it one command mongodb book”})

db.<name of collection>.find() To see all the documents of a Db.books.find();


collections
Db.collectionName.update({selection To update a document in a collection db.Books.update({‘Name’:’Bill
criteria, updated data}) Vanners’}, {$set:{‘Name’: ‘Inside
JVM’}})
Db.collection.drop() To drop a collection from database Db.lib.drop();
Db.collectionName.remove(“criteria”) Remove a document from collection Db.Books.remove({‘Name’:’ABC’})
Accessing MongoDB from Node.js (Data
Retrieval)
• Mongoose : Mongodb Object Modeling for node.js
• 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.

You might also like