[go: up one dir, main page]

0% found this document useful (0 votes)
3 views6 pages

11 MongoDB Lab

The document outlines an assignment to implement a MongoDB database for an institute, focusing on CRUD operations. It details the structure of a Student collection, including various attributes and operations such as creating, updating, and deleting records. Additionally, it provides theoretical explanations of basic MongoDB commands and logical operators for querying data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

11 MongoDB Lab

The document outlines an assignment to implement a MongoDB database for an institute, focusing on CRUD operations. It details the structure of a Student collection, including various attributes and operations such as creating, updating, and deleting records. Additionally, it provides theoretical explanations of basic MongoDB commands and logical operators for querying data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Department of Information Technology

Database Programming
Assignment Number: 11
Tittle:
Implement a database using MongoDb and to learn CRUD operations
.
Problem statement :
Create an Institute Database and Create Student collection with following keys
1. Student Id
2. Student Name
3. Branch
4. City
5. Subjects :[
{ subject name: score: }
{ subject name: score:
} ]
6. Area of Interest : [“Block Chain”,”AI”, . .]
7. prev_sem_percentage
8.DOB

* Eg : Enter Subject Names as : 1. DSA 2. TOC

1. Create database Institute.


2. Create collection Students.
3. Insert 10 document with above mentioned structure (Key subjects need to be in only 3
documents)
4. Display all students information.
5. Update student branch from IT to Computer of studentid 3.
6. Display students staying in Pune city.
7. Display students staying in Pune or Mumbai City.
8. Add interest Robotics to studentid 5.
9. Display students with branch IT and area of interest with “AI”
10. Change City name from Madras to Chennai.(for all occurances)
11. Remove record with student id 3.
12. Add new Key Hobbies with values for Student id 7.
13. Display students with area of interest Robotics and MongoDB.
14. Create and Drop a temporary collection .
15. Display total number of documents
16. Display how many students are from “Delhi”
17. Display top 3 students name based on their previous sem percentage.
18. Display students having score greater than 50 and less than 70 in TOC.
19. Display students name and previous sem percentage with highest scorer on top.
Objective:
1. To Understand Design of NoSQL database.
2. To understand and execute CRUD commands of MongoDb

Theory:
Basic Operations with the Shell:
To Start the Mongodb server on the terminal , traverse till bin folder of Monodb folder and run
following command
>./mongod --dbpath <path of the folder along with folder name where U want to store the
database>

You will see “waiting for connection “ message at the server terminal.

Open another terminal, traverse till bin folder of Monodb folder and run following command
>./mongo
>
You will see connection:1 at the server terminal

>show dbs
Prints a list of all databases

>Use DatabaseName;
The above command create new database or if it already exists shifts perspective to that
database

>show collections
Will list all the collections under current database.

We can use the four basic operations create, read, update and delete (CRUD) to manipulate and
view data at the shell.

1.Create: The insert function adds a document to a collection.


For example, suppose we want to store a blog post. First, we’ll create a local variable called
post that is a JavaScript object representing our document. It will have the keys "title",
"content" and "date". (the date that it was published):

>post = {"title" : "My Blog Post", "content" : "Here's my blog post.","date" : new Date()}

"title" : "My Blog Post",


"content" : "Here's my blog post.",
"date" : ISODate("2012-08-24T21:12:09.982Z")

This object is a valid MongoDB document, so we can save it to the blog collection using the
insert method:

>db.blog.insert(post)

2. Read:
Find and findOne can be used to query a collection. If we just want to see one document from
a collection, we can use findOne.

>db.blog.findOne()

{
"_id" :ObjectId("5037ee4a1084eb3ffeef7228"),
"title" : "My Blog Post",
"content" : "Here's my blog post.",
"date" : ISODate("2012-08-24T21:12:09.982Z")
}

Find and findOne can also be passed criteria in the form of a query document. This will restrict
the documents matched by the query. The shell will automatically display up to 20 documents
matching a find, but more can be fetched.

3.Update:
db.collection.update(query, update, options)
Modifies an existing document or documents in a collection. The method can modify
specific fields of an existing document or documents or replace an existing document
entirely, depending on the update parameter.
By default, the update() method updates a single document. Set the Multi
Parameter to update all documents that match the query criteria.
The update() method has the following form:
db.collection.update(
<query>,
<update>,
{
upsert:<boolean>,
multi:<boolean>,
writeConcern:<document>
}
)

4.Delete:
db.collection.remove({})

Removes documents from a collection.


The db.collection.remove() method can have one of two syntaxes. The remove() method can
take a query document and an optional justOne boolean:

db.collection.remove(
<query>,
<justOne>
)

Or the method can take a query document and an optional remove options document:

Delete permanently deletes documents from the database. Called with no parameters, it
removes all documents from a collection. It can also take a document specifying criteria for
removal. For example, this would remove the post we just created:

>db.blog.DeleteOne({title : "My Blog Post"})

The MongoDB $and operator performs a logical AND operation on an array of two or more
expressions and retrieves the documents which satisfy all the expressions in the array. The $and
operator uses short-circuit evaluation. If the first expression (e.g. <expression1>) evaluates to
false, MongoDB will not evaluate the remaining expressions.
Syntax:
{ $and: [ { <exp1> }, { <exp2> } , ... , { <expN> } ] }
Example of MongoDB Logical Operator - $and
If we want to select all documents from the collection "student" which satisfying the condition -
1. sex of student is Female and
2. class of the student is VI and
3. grd_point of the student is greater than equal to 31
The following mongodb command can be used :
>db.student.find({$and:[{"sex":"Male"},{"grd_point":{ $gte: 31 }},{"class":"VI"}]}).pretty();
Logical Operator - $not
The MongoDB $not operator performs a logical NOT operation on the given expression and
fetches selected documents that do not match the expression and the document that do not
contain the field as well, specified in the expression.
Syntax:
{ field: { $not: { <expression> } } }
Example of MongoDB Logical Operator - $not
If we want to select all documents from the collection "student" which satisfying the condition -
age of the student is at least 12
the following mongodb command can be used :
>db.student.find( {"age": { $not: {$lt : 12}}}).pretty();
Example of MongoDB Logical Operator - $not with pattern matching
If we want to select all documents from the collection "student" which satisfying the condition -
sex of student must not Male.
the following mongodb command can be used :
>db.student.find( {"sex": { $not: /^M.*/}}).pretty();
Logical operator $or and $nor.
The $or operator is used to search multiple expression in a single query with only one matching
criterion to be needed in a document. More than one keys and values can be used with the $or
operator.
MongoDB conditional operator - $or example
If we want to fetch documents from the collection "testtable" which containing the value of "age
" either 19 or 22 or 23, the following mongodb command can be used :
>db.testtable.find({$or : [{"age" : 19},{"age" : 22},{"age":23}]}
MongoDB $or operator with another field
If we want to fetch documents from the collection "testtable" which containing the value of
"date_of_join" is "16/10/2010" and the value of "age " either 19 or 22 or 23, the following
mongodb command can be used :
>db.testtable.find( { "date_of_join" : "16/10/2010" , $or : [{"age" : 19},{"age" : 22},{"age":23}]
})
The $nor operator is used to search multiple expression in a single query which does not match
any of the values specified with the $nor.
MongoDB $nor(not or ) operator
If we want to fetch documents from the collection "testtable" which containing the value of
"date_of_join" is "16/10/2010" and not containing the value of "age " either 19 or 22 or 23, the
following mongodb command can be used :
>db.testtable.find( { "date_of_join" : "16/10/2010" , $nor : [{"age" : 19},{"age" :22},{"age":23}]
})
Conclusion

You might also like