0% found this document useful (0 votes)
8 views

Dbms Assignment 9

The document discusses installing and studying MongoDB to implement CRUD operations. It explains MongoDB concepts like collections and documents, and how CRUD operations map to database functions. It provides examples of using MongoDB's insert(), find(), update(), and remove() methods to create, read, update, and delete documents in a collection.

Uploaded by

nikita.aher
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Dbms Assignment 9

The document discusses installing and studying MongoDB to implement CRUD operations. It explains MongoDB concepts like collections and documents, and how CRUD operations map to database functions. It provides examples of using MongoDB's insert(), find(), update(), and remove() methods to create, read, update, and delete documents in a collection.

Uploaded by

nikita.aher
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Group B

Assignment No: 9
Class: T.E. Computer Roll No:

Aim: Install & study MongoDB and Implement CRUD Operations.

Problem Statement:
Study of Open Source NOSQL Database: MongoDB (Installation, Basic CRUD operations,
Execution)

Objective:
1. To learn and understand NOSQL Database.
2. To execute CRUD Operations on MongoDB.
Hardware requirements:
 Any CPU with Pentium Processor or similar, 256 MB
 RAM or more, 1 GB Hard Disk or more.
Software requirements:
Ubuntu 14.04, Mongodb Packages
Theory:
MongoDB is a free and open-source NoSQL document database used commonly in modern web
applications. MongoDB works on concept of collection and document.

Collection
Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table. A
collection exists within a single database. Collections do not enforce a schema.

Document
A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema
means that documents in the same collection do not need to have the same set of fields or
structure, and common fields in a collection's documents may hold different types of data.

CRUD Operations:

CRUD operations create, read, update, and delete documents.


These operations are considered to be the four basic functionalities of a repository .

CRUD operations can be mapped directly to database operations:

 Create matches insert


 Read matches select
 Update matches update
 Delete matches delete

Create Operations

Create or insert operations add new documents to a collection. If the collection does not currently
exist, insert operations will create the collection.

db.collection.insert()

Syntax:

db.collection.insert(

<document or array of documents>,

writeConcern: <document>,

ordered: <boolean>

e.g.

1) db.products.insert( { item: "card", qty: 15 } )

Will store record(document) as

{ "_id" : ObjectId("5063114bd386d8fadbd6b004"), "item" : "card", "qty" : 15 }

2) db.products.insert( { _id: 10, item: "box", qty: 20 } )

Will store record (document) as


db.products.insert( { _id: 10, item: "box", qty: 20 } )

Read Operations

Read operations retrieves documents from a collection; i.e. queries a collection for documents.
MongoDB provides the following methods to read documents from a collection:

 db.collection.find()

You can specify query filters or criteria that identify the documents to return.

Examples

Find All Documents in a Collection

The find() method with no parameters returns all documents from a collection and returns all fields
for the documents. For example, the following operation returns all documents in the bios
collection:

db.bios.find()

Find Documents that Match Query Criteria

To find documents that match a set of selection criteria, call find() with the <criteria> parameter.
The following operation returns all the documents from the collection products where qty is greater
than 25:

db.products.find( { qty: { $gt: 25 } } )

Update Operations

MongoDB Update() Method:

The update() method updates the values in the existing document.

Syntax
The basic syntax of update() method is as follows −

>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)
Example
Consider the mycol collection has the following data.

{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}


{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}

Following example will set the new title 'New MongoDB Tutorial' of the documents whose title is
'MongoDB Overview'.

>db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}})


>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"New MongoDB Tutorial"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
>

By default, MongoDB will update only a single document. To update multiple documents, you
need to set a parameter 'multi' to true.

>db.mycol.update({'title':'MongoDB Overview'},
{$set:{'title':'New MongoDB Tutorial'}},{multi:true})

Delete Operation:

The remove() Method


MongoDB's remove() method is used to remove a document from the collection. remove() method
accepts two parameters. One is deletion criteria and second is justOne flag.

 deletion criteria − (Optional) deletion criteria according to documents will be removed.

 justOne − (Optional) if set to true or 1, then remove only one document.

Syntax
Basic syntax of remove() method is as follows −

>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)

Example
Consider the mycol collection has the following data.
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}

Following example will remove all the documents whose title is 'MongoDB Overview'.

>db.mycol.remove({'title':'MongoDB Overview'})
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
>

Remove Only One


If there are multiple records and you want to delete only the first record, then
set justOne parameter in remove() method.

>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)

Remove All Documents


If you don't specify deletion criteria, then MongoDB will delete whole documents from the
collection. This is equivalent of SQL's truncate command.

>db.mycol.remove()
>db.mycol.find()
>

Conclusion:

Oral Questions:
1)Difference between SQL & NoSQL.
2) Which are features of NoSQL Database.
3)Which are different types of NoSQL Database( Refer 6th unit)
4)How to insert record in mongodb collection with _id & without _id.
5)how to display document having item name as ‘pen’ from products database.

You might also like