MongoDB Lecture 2
MongoDB Lecture 2
USING MONGODB
by
Manya Gidwani
Assistant Professor
IT Department SAKEC
CONTENTS
MongoDB user accounts and access control
MongoDB SET UP
CRUD Operations
MONGODB USER ACCOUNTS AND ACCESS
CONTROL
A : Bulit in Roles: Role based authorization is enforced via built-in roles that give you
several levels of access to the database. Role can give several privileges to perform
tasks for a resource available. The categories are:
1. User Database: offers read or read and write permission to database
2. Database Administration: offers admin privileges like updating the schema and
objects within it
3. Cluster Administration : whole mongodb system admin including replicas and
sharding
4. Backup and restore: responsible for backup, maintenance activities and to cache
data for restoration.
5. All database roles: access to all databases within the schema.no access to config
files
6. Superuser: can gran any level of access to any user .this is root access.
:
MONGODB USER ACCOUNTS AND
ACCESS CONTROL
B :User Defined Roles: Mongodb allows to create
our own roles according to our needs and the need of
the roles who would use the data within databese.
#1. db.createRole(role, writeConcern) : The createRole
method is used to assign a role under the database.
Using this method, we can specify privileges for the
role by explicitly listing the privileges.
#2. db.dropRole(rolename, writeConcern):The drop
role method is used to remove the specified user-
defined role from the database on which we are
running the method.
#3. db.getRole(rolename, args):
MONGODB USER ACCOUNTS AND
ACCESS CONTROL
? User defined role actions
1. Database management actions
2. Querying actions
3. Deployment Management actions
4. Replication Actions
5. Server administration actions
6. Sharding Actions
7. Session Actions
8. Monitoring actions
? The use Command in MongoDB is used to create database.
If you want to check your databases list, then use the command show dbs.
>show dbs
Output:
admin 0.000GB
config 0.000GB
local 0.000GB
mydb 0.000GB
? Create Collection (C- Create)
db.createCollection(name, options)
>db.createCollection(“operate")
Output: { "ok" : 1 }
>show collections
Output: operate
Insert Records
? Insert one Record
>db.operate.insert({roll_no:"1",name:"Jain Kunal",college:"SAKEC"})
Output: WriteResult({ "nInserted" : 1 })
? Insert Many Records
>db.operate.insertMany([{roll_no:"3",name:"Shah
Nimal",college:"SAKEC"},{roll_no:"4",name:"Shah
Mayank",college:"VESIT"}])
Output: {
"acknowledged" : true,
"insertedIds" : [
ObjectId("6253ffd71d7ae01dd0241274"),
ObjectId("6253ffd71d7ae01dd0241275")
]
}
_id is 12 bytes hexadecimal number unique for
every document in a collection.
? To insert multiple documents in single query, you can also pass
an array of documents in insert() command.
? The find( ) method for (R-Read operation): This method is
used for querying data from a MongoDB collection.
The basic syntax for using this method is
? db.collection_name.find()
? Example: db.operate.find()
Output:
{ "_id" : ObjectId("6253ff051d7ae01dd0241272"), "roll_no" :
"1", "name" : "Jain Kunal", "college" : "SAKEC" }
{ "_id" : ObjectId("6253ff5a1d7ae01dd0241273"), "roll_no" :
"2", "name" : "Vora Kush", "college" : "SAKEC" }
{ "_id" : ObjectId("6253ffd71d7ae01dd0241274"), "roll_no" :
"3", "name" : "Shah Nimal", "college" : "SAKEC" }
{ "_id" : ObjectId("6253ffd71d7ae01dd0241275"), "roll_no" :
"4", "name" : "Shah Mayank", "college" : "VESIT" }
? The pretty() method: This method is used for giving a proper
format to the output extracted by the query.
The basic syntax for using this method is:
? db.collection_name.find().pretty()
Example: db.operate.find().pretty()
? Output:
Output:
{
"_id" : ObjectId("6253ff051d7ae01dd0241272"),
"roll_no" : "1",
"name" : "Jain Kunal",
"college" : "SAKEC"
}
{
"_id" : ObjectId("6253ff5a1d7ae01dd0241273"),
"roll_no" : "2",
"name" : "Vora Kush",
"college" : "SAKEC"
}
{
"_id" : ObjectId("6253ffd71d7ae01dd0241274"),
"roll_no" : "3",
"name" : "Shah Nimal",
"college" : "SAKEC"
}
{
"_id" : ObjectId("6253ffd71d7ae01dd0241275"),
"roll_no" : "4",
"name" : "Shah Mayank",
"college" : "VESIT"
}
FILTERING IN MONGODB QUERIES
? It is also possible to filter your results by
giving or adding some specific criteria in
which you are interested to
>db.operate.find({"name":"Jain Kunal"})
Output { "_id" :
ObjectId("6253ff051d7ae01dd0241272"),
"roll_no" : "1", "name" : "Jain Kunal", "college" :
"SAKEC" }
FILTERING IN MONGODB QUERIES
? MongoDB also allows you in specifying data
values of the documents holding two or more
specified values to be fetched from the query
(AND condition)
⮚ db.operate.find({name:"Jain Kunal",
roll_no:"1"})
Output:
{ "_id" : ObjectId("6253ff051d7ae01dd0241272"),
"roll_no" : "1", "name" : "Jain Kunal", "college" :
"SAKEC" }
FILTERING IN MONGODB QUERIES
? MongoDB allows users to specify either one or
multiple values to be true. According to this, till one
of the conditions is true, the document data will get
returned. Here is an example showing the use of
OR condition:
⮚ db.operate.find({ $or:[{"roll_no": "1"}, {"college":
"VESIT"}]})
⮚ Output:
{ "_id" : ObjectId("6253ff051d7ae01dd0241272"),
"roll_no" : "1", "name" : "Jain Kunal", "college" :
"SAKEC" }
{ "_id" : ObjectId("6253ffd71d7ae01dd0241275"),
"roll_no" : "4", "name" : "Shah Mayank", "college" :
"VESIT" }
FILTERING IN MONGODB QUERIES
? The $in operator is another special operator used in
queries for providing a list of values in the query.
When your document holds any of those provided
values, it gets returned. Here is an example:
>db.operate.find( {"college" : { $in: ["SAKEC"]}})
Output:
{ "_id" : ObjectId("6253ff051d7ae01dd0241272"),
"roll_no" : "1", "name" : "Jain Kunal", "college" :
"SAKEC" }
{ "_id" : ObjectId("6253ff5a1d7ae01dd0241273"),
"roll_no" : "2", "name" : "Vora Kush", "college" :
"SAKEC" }
{ "_id" : ObjectId("6253ffd71d7ae01dd0241274"),
"roll_no" : "3", "name" : "Shah Nimal", "college" :
"SAKEC" }
MONGODB PROJECTION QUERIES
? Protection queries are a particular type of MongoDB
queries where you can specify fields you want to get in
the output.
? MongoDB allows you to perform a query for a collection
by using the db.collection.find() method, where you
have to mention the field that needs to be explicitly
returned.
? This can be done by explicitly incorporating the field
names in your query, and adding a 1 or 0 with them for
specifying whether this needs to be returned or not.
? Such kinds of parameters are called projection
parameter. When a projection parameter is associated
with a value 1, it will show the value according to the
query and hide when the projection parameter has a
value 0.
MONGODB PROJECTION QUERIES
? Example
>db.operate.find({"name":"Jain Kunal"},{_id:0,
name:1,college:1})
Output:
{ "name" : "Jain Kunal", "college" : "SAKEC" }
MONGODB LIMITING QUERY RESULT
? MongoDB allows you to specify the maximum number of documents to return by making
use of the limit() method which will return only the number of documents you need.
? And as soon as you prepare a MongoDB query for the collection with the help of
db.collection.find() method, you can add on the limit() method for specifying the limit.
? Example
>db.operate.find().pretty().limit(2)
Output:
{
"_id" : ObjectId("6253ff051d7ae01dd0241272"),
"roll_no" : "1",
"name" : "Jain Kunal",
"college" : "SAKEC"
}
{
"_id" : ObjectId("6253ff5a1d7ae01dd0241273"),
"roll_no" : "2",
"name" : "Vora Kush",
"college" : "SAKEC"
}
MONGODB UPDATE DOCUMENTS
? The update operation (U- Update) in a database is
used to change or update values in a document.
? MongoDB makes use of the update() method for
updating the documents within a MongoDB
collection.
? For updating any specific documents, a new
criterion can be incorporated with the update
statement, which will only update the selected
documents.
MONGODB UPDATE DOCUMENTS
? You have to put some specific condition in the form
of the parameter to update the document in
MongoDB. Here is a stepwise representation of how
this can be performed:
? Make use of the update() method.
1. Prefer the circumstance that you wish to
implement for deciding which document needs an
update in their database. Let us assume an
example where you want to update your
document which is having an id 4.
2. Then make use of the set command for modifying
the Field Name.
3. Select which Field Name you wish for modifying
and go into the new value consequently.
MONGODB UPDATE DOCUMENTS
? Example
> db.operate.update({"roll_no":"1"} ,{$set: {"name":
"Jain Kunal Manoj"}})
Output:
WriteResult({ "nMatched" : 1, "nUpserted" : 0,
"nModified" : 1 })
MONGODB DELETE METHOD
? After creating an updating the document, a situation might occur
where you want to delete any document or a set of documents.
? MongoDB also allows you to delete any particular document or
multiple collections of documents. documents from a MongoDB
database.
? db.collection_name.deleteOne() (D-delete) : This method is used
to delete only a single document, even when more than one
document matches with the criteria. Here is an example of using
this db.collection.deleteOne() method for deleting the single
document
? Exp: > db.operate.deleteOne({"name":"Vora Kush"})
? db.collection.deleteMany() : This method deletes all your
documents whichever match its criteria mentioned in the
parameter
? Exp: >db.programmers.deleteMany( { name: { $in: [ "Dennis
Ritchie", "Bjarne Stroustrup" ] } } )
? The save() method replaces the existing document
with the new document passed in save() method
? Syntax
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DA
TA})
? The remove() Method is used to remove
document from the collection. remove() method
accepts two parameters. One is deletion criteria and
second
Thank
You