0% found this document useful (0 votes)
13 views41 pages

11 SESION - MongoDB Commands and CRUD Operations I

The document outlines a training session on MongoDB commands and CRUD operations, focusing on unstructured data management. It covers installation of MongoDB Shell, basic commands, and detailed instructions for creating, reading, and inserting documents in a MongoDB collection. The session also includes examples of various query operations to find documents based on specific criteria.

Uploaded by

rosecamargo227
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)
13 views41 pages

11 SESION - MongoDB Commands and CRUD Operations I

The document outlines a training session on MongoDB commands and CRUD operations, focusing on unstructured data management. It covers installation of MongoDB Shell, basic commands, and detailed instructions for creating, reading, and inserting documents in a MongoDB collection. The session also includes examples of various query operations to find documents based on specific criteria.

Uploaded by

rosecamargo227
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/ 41

DATA1047A – Data Management II:

Unstructured Data

MongoDB Commands and


CRUD Operations I

Instructor: Israel Martinez


October 4th, 2023.
Agenda

Installing MondoDB Shell from MongoDB Atlas


Basic MongoDB Commands
CRUD Operations
Inserting Documents in a MongoDB collection
Finding Documents in a MongoDB collection
References
Installing MongoDB Shell from
MongoDB Atlas
Installing MongoDB Shell from MongoDB Atlas

• Log in to your Atlas account:

• Log in | MongoDB

• Once logged in, connect to cluster


Installing MongoDB Shell from MongoDB Atlas
Step 3: Select Shell Option
Step 4: Select “I do not have MongoDB shell installed”
Step 5: From “Install from .zip file”, follow sub-steps 2
and 3

Install mongosh — MongoDB Shell


Some Basic Commands
Some Basic Commands

• To display the database you are using, type db:

> db

• The operation should return test, which is the default database.

• To switch databases, issue the use <db> helper, as in the following example:

> use <database>

• To list the databases available to the user, use the helper:

> show dbs.


Create a New Database and Collection

• To create a new database, issue the use <db> command with the database
that you would like to create. Example

> use myNewDatabase

• To create a new collection, you can use the insertOne() operation:

> db.myCollection.insertOne( { x: 1 } );

• If a collection does not exist, MongoDB creates the collection when you
first store data for that collection.
More Commands

• To show all collections in a database:

>show collections

• To clear the console:

>cls

• Alternatively:
>console.clear()
More Commands

• To terminate a running command:

➢ <Ctrl + C>

• Press twice <Ctrl + C> to exit mongosh

• Or alternatively

>exit
CRUD Operations
CRUD Operations

• CRUD operations create, read, update, and delete


documents.

• In this session we are going to focus on create and read


operations

• Tomorrow we will focus on update and delete operations


Create Operations

• Create or insert operations add new documents to a


collection.

• If the collection does not exist, create operations also


create the collection.

• You can insert a single document or multiple documents in


a single operation.
Read Operations

• Read operations retrieve documents from a collection

• Example: query a collection for documents.

• You can specify criteria, or filters, that identify the


documents to return.
Inserting Documents in a
MongoDB Collection
Insert a Single Document

• The insertOne() command inserts a document into a


collection.

• Within the parentheses of insertOne(), include an object


that contains the document data.
Example

db.grades.insertOne({
student_id: 654321,
products: [
{
type: "exam",
score: 90,
},
{
type: "homework",
score: 59,
},
{
type: "quiz",
score: 75,
},
{
type: "homework",
score: 88,
},
],
class_id: 550,
})
Insert Multiple Documents

• Use insertMany() to insert multiple documents at once.

• Within insertMany(), include the documents within an


array.

• Each document should be separated by a comma.


Example
db.grades.insertMany([
{ {
student_id: 546789, student_id: 777777,
products: [ products: [
{ {
type: "quiz", type: "exam",
score: 50, score: 83,
}, },
{ {
type: "homework", type: "quiz",
score: 70, score: 59,
}, },
{ {
type: "quiz", type: "quiz",
score: 66, score: 72,
}, },
{ {
type: "exam", type: "quiz",
score: 70, score: 67,
}, },
], ],
class_id: 551, class_id: 550,
}, },
Example (continuation)
{
student_id: 223344,
products: [
{
type: "exam",
score: 45,
},
{
type: "homework",
score: 39,
},
{
type: "quiz",
score: 40,
},
{
type: "homework",
score: 88,
},
],
class_id: 551,
},
])
Finding Documents in a
MongoDB Collection
Read All Documents in a Collection

• To read all documents in the collection, pass an empty


document as the query filter parameter to the find()
method.

• The query filter parameter determines the select criteria.


Read All Documents in a Collection

To return all documents from the grades collection:

db.grades.find()
Find a Document with Equality

• When given equality with an _id field, the find() command


will return the specified document that matches the _id.

• Example:

db.grades.find({ _id:ObjectId("651ccbab0023e1dfba073da8")
})
Find a Document with Equality

• In addition, the find() command can work with any other


field.

• Example:

db.grades.find({ class_id: 550})


Find a Document by Using the $in Operator

• The $in operator is used to select documents where the


value of a field equals any value in the specified array.

• Example:

db.grades.find({ class_id: { $in: [550, 551] } })


Finding Documents by Using Comparison Operators

• Comparison operators:

• $gt
• $lt
• $lte
• $gte
The $gt Comparison Operator

• The $gt operator matchs documents with a field greater


than the given value.

• Example:

• db.grades.find({"products.score": { $gt: 60}})


The $lt Comparison Operator

• The $lt operator matchs documents with a field less than


the given value.

• Example:

• db.grades.find({"products.score": {$lt: 60}})


The $lte Comparison Operator

• The $lte operator matchs documents with a field less than


or equal to the given value.

• Example:

• db.grades.find({"products.score": { $lte: 70}})


The $gte Comparison Operator

• The $gte operator matchs documents with a field greater


than or equal to the given value.

• Example:

• db.grades.find({"products.score": { $gte: 65}})


Find a Document by Using the $elemMatch Operator

• The $elemMatch operator can be used to find all


documents that contain the specified subdocument. For
example:

db.grades.find({
products: {
$elemMatch: { type: "exam", score: { $gt: 60 }},
},
})
Finding Documents by Using Logical Operators

• Logical operators:

• implicit $and
• $or
• $and
Find a Document by Using Implicit $and

• By using implicit $and, you can select documents that


match multiple expressions.

• Example:

• db.grades.find({"class_id": 551, "products.score": { $gte:


60 } })
Find a Document by Using the $or Operator

• The $or operator can be used to select documents that


match at least one of the included expressions.

• Example:

db.grades.find({
$or: [{"products.type": "quiz" }, {"product.score" : 70 }],
})
Find a Document by Using the $and Operator

• The $and operator can be used for multiple $or expressions in


your query.

db.grades.find({
$and: [
{ $or: [{"products.type" : "quiz" }, {"products.score" : 70}] },
{ $or: [{"class_id": 550 }, {"student_id" : 654321}] },
]
})
Next Session

• MongoDB CRUD Operations II


This lecture was based on these sources:

A Comprehensive Guide To Data Modeling | MongoDB

Identifying Database Workloads - Learn | MongoDB

Modeling Data Relationships - Learn | MongoDB

Images credit: A Comprehensive Guide To Data Modeling | MongoDB

You might also like