0% found this document useful (0 votes)
67 views11 pages

Working With MongoDB - 518

This document discusses various CRUD operations in MongoDB such as creating, reading, updating, and deleting databases, collections, and documents. It provides examples of how to [1] create and switch between databases, [2] insert documents into collections using methods like insert(), insertOne(), and insertMany(), [3] find documents using the find() method, [4] update documents using methods like update(), updateOne(), updateMany(), and replaceOne(), and [5] delete documents using remove(), deleteOne(), and deleteMany(). The document aims to teach how to perform basic CRUD functions in MongoDB.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views11 pages

Working With MongoDB - 518

This document discusses various CRUD operations in MongoDB such as creating, reading, updating, and deleting databases, collections, and documents. It provides examples of how to [1] create and switch between databases, [2] insert documents into collections using methods like insert(), insertOne(), and insertMany(), [3] find documents using the find() method, [4] update documents using methods like update(), updateOne(), updateMany(), and replaceOne(), and [5] delete documents using remove(), deleteOne(), and deleteMany(). The document aims to teach how to perform basic CRUD functions in MongoDB.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Birla Institute of Technology and Science, Pilani

SS ZG518 Database Design and Applications


Lab No # 9 B
*************************************************************************************

1 Working with MongoDB

After successfully completing the installation of MongoDB.

Let’s learn how to work on this non-relational Database system.

5.1 To know the existing databases:


Using ‘show dbs;’. Initially, it shows the list of default databases as shown below.

5.2 Creating a new Database:

For creating a new database:


Syntax->> use database_name
Example->> use xyzdb
Databases are not shown until we add some collections to the database.

Here, after switching the database to xyzdb, xyzdb is still not available in the list of databases.

5.3 Creating Collections

In MongoDB we have various databases and these databases have various collections.

A collection is a group of documents.

There are 2 ways we can create collections in databases.

A) Using the createCollection() method:

Syntax: db.createCollection(“collection_name”)

Example: db.createCollection(“employee”)

This will create a collection as “employee”

Now inserting documents into the collection “employee”.

Syntax: db.collection_name.insert({"field_name":"field_value"})

Example: db.employee.insert({"empname":"emma"})
After inserting documents into the collection, the name of the database inside which the
collection exists, can be seen in the list of databases.
B) Another way to create a collection is the direct way.

Syntax ->> db.collection_name.insert({“Field_name”:”Field_value”})


Example ->> db.department.insert({"deptname":"operations"})

This will insert the field name and its respective value in a collection with ‘department’ as name.

NOTE: The database will be available only after we add some collections to it.

To look for the collections in the database use ‘show collections’.

Here the database is ‘xyzdb’ and inside the database we have a 2 collection: employee and
department.

5.4 Retrieve a document from Collection

To retrieve the documents inside a collection use the method find().

Syntax: db.collection_name.find()

This gives the documents inside a particular collection. MongoDB automatically adds a unique
id field to the document.
5.5 Drop a collection from Database

To delete a collection from the database

Use the drop() command.

Syntax: db.collection_name.drop()

5.6 Drop a Database

Use command dropDatabase().

This will delete the database as well as the collections inside the database.
5.2 CRUD Operations in MongoDB

CRUD operations are to Create, Read, Update, and Delete Documents.

5.2.1 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.

MongoDB provides the following methods to insert documents into a collection:

● Using insert() method.

db.collection_name.insert({field_name: field_value})

Here creating and inserting 2 documents into the collection ‘employee’ inside the database
xyzdb.

● Using db.collection.insertOne(), inserts a single document into a collection.

Run the following to insert a single document into the inventory collection :
To retrieve the document that you just inserted, query the collection with:
db.department.find()

● db.collection.insertMany() can insert multiple documents into a collection. Pass an array


of documents to the method.
Run the following to insert multiple document into the inventory collection :
Here, inserting several entries together inside the collection ‘client’ using insertMany()
command.
To Read operations retrieve documents from a collection; i.e. query a collection for documents.

MongoDB provides the following methods to read documents from a collection:

● db.collection.find()

5.2.3 Update Operations

Update operations modify existing documents in a collection. MongoDB provides the following
methods to update documents of a collection:

● Using the update() method

● db.collection.updateOne()

The following example uses the db.collection.updateOne() method on the inventory collection to
update the client name where value is “abc Tech” to “ABC Tech”.
Here the update operation uses the ‘$set’ operator to update the value of the client name.

● db.collection.updateMany()

This method is used to update the selected values at once. Here, the selected value gets changed
at all places.
If we had 2 instances for client name: “KK Tech”, both of their departments would be updated to
“Accounting”.

Here the update operation uses the ‘$set’ operator to update the value of the client department to
“Accounting”.

● db.collection.replaceOne()

This method replaces a selected field name to another name.


Here, in the client collection, the client name was replaced as ‘AABC’ from ‘ABC’.
5.2.3 Delete Operations
Delete operations remove documents from a collection. MongoDB provides the following
methods to delete documents of a collection.
● Remove documents from the collection.

Use the remove() method to delete a particular collection with a specific field value.
Here, a document is deleted from the employee collection with empid= 25.

● db.collection.deleteOne()

Here, only one specific document is deleted at a time. In the below example, the document is
deleted using its unique id value.
db.collection.deleteMany()

To Delete All Documents specified.


Here, all the documents in the employee collection with empid=25 were deleted all at once.

Note the count of deleted documents = 3.

***********************************END***************************************

You might also like