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

slides01-15-30

The document provides an overview of JSON and BSON, highlighting their structure, types, and usage in MongoDB. It explains CRUD operations, including methods for creating, reading, updating, and deleting documents, and emphasizes the importance of unique IDs and embedded documents. Additionally, it outlines tasks related to patient data management and summarizes key concepts of databases, collections, and document structure in MongoDB.

Uploaded by

Pawan Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

slides01-15-30

The document provides an overview of JSON and BSON, highlighting their structure, types, and usage in MongoDB. It explains CRUD operations, including methods for creating, reading, updating, and deleting documents, and emphasizes the importance of unique IDs and embedded documents. Additionally, it outlines tasks related to patient data management and summarizes key concepts of databases, collections, and document structure in MongoDB.

Uploaded by

Pawan Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

JSON

{ This is called a “Field” or


"name": "Max", “Property” of the JSON
"age": 29, document. Multiple Fields
are separated by commas
"isInstructor": true,
"hobbies": [ Key Value “Fields” consist of a “Key”
(or “name”) and “Value”
Surrounding curly "Sports", part. "Key and Value are
braces delimit the "Cooking" separated by a colon.
JSON document
],
"address": {
"street": "My Street 5",
"city": "Munich" Values can be strings (e.g.
} “Max”), numbers (e.g. 29),
booleans (e.g. true), arrays
} ([ … ]) and other documents
(also called objects; { … })
JSON vs BSON

JSON BSON

{
"name": "Max", MongoDB
Binary Data
"age": 29 Drivers
}

Extends JSON Types (e.g. more


detailed Number Types)

Efficient Storage
CRUD Operations & MongoDB

Application Analytics / BI Tools Admin

User

<Code>

MongoDB Driver BI Connector / Shell Shell

Read
Create, Read, Update, Delete Create, Read, Update, Delete

MongoDB Server

Collection A Collection B

{ … }, { … } { … }, { … }
CRUD Operations

Create Update
insertOne(data, options) updateOne(filter, data, options)

insertMany(data, options) updateMany(filter, data, options)

replaceOne(filter, data, options)

Read Delete
find(filter, options) deleteOne(filter, options)

findOne(filter, options) deleteMany(filter, options)


Example #1: Flight Data

Flight

Schedule
Create Delete Cancel Flight
(Add) a Flight

{
"departureAirport": "MUC",
Update Display Flight
Update "arrivalAirport": "SFO", Read
Information Information
"aircraft": "Airbus A380",
"distance": 12000,
"intercontinental": true
},
{ … }
Unique IDs

You MUST have an _id

MongoDB creates an ObjectId() for you

You can set any other Value


Embedded Documents

Up to 100 Levels
of Nesting

Max. 16mb /
document
Documents
Arrays
Embedded
Array of

Arrays can hold ANY Data


Cursors

find()

[ { … }, { … }, … ] Cursor Object

Cycle Through Results


Projection

In Database
{
"_id": "...",
"name": "Max",
"age": 29,
"job": "instructor"
}

Projection

In Application
{
"name": "Max",
"age": 29
}
update() vs updateOne() vs updateMany()

update() updateOne() updateMany()

Error without $set (or Error without $set (or


Overwrite by default
other update operators) other update operators)

Use $set to patch values Use $set to patch values Use $set to patch values

Update all identified Update first identified Update all identified


elements element elements

Use these!
Example #2: Patient Data

Patient

{
"firstName": "Max",
"lastName": "Schwarzmueller",
"age": 29,
"history": [
{ "disease": "cold", "treatment": … },
{ … }
]
}
Tasks

1 Insert 3 patient records with at least 1 history entry per patient

2 Update patient data of 1 patient with new age, name and history entry

3 Find all patients who are older than 30 (or a value of your choice)

4 Delete all patients who got a cold as a disease


Module Summary

Databases, Collections, Documents CRUD Operations

§ A Database holds multiple Collections


§ CRUD = Create, Read, Update, Delete
where each Collection can then hold
§ MongoDB offers multiple CRUD operations for
multiple Documents
single-document and bulk actions (e.g.
§ Databases and Collections are created
insertOne(), insertMany(), …)
“lazily” (i.e. when a Document is
§ Some methods require an argument (e.g.
inserted)
insertOne()), others don’t (e.g. find())
§ A Document can’t directly be inserted
§ find() returns a cursor, NOT a list of documents!
into a Database, you need to use a
§ Use filters to find specific documents
Collection!

Document Structure Retrieving Data

§ Each document needs a unique ID (and


§ Use filters and operators (e.g. $gt) to limit the
gets one by default)
number of documents you retrieve
§ You may have embedded documents
§ Use projection to limit the set of fields you retrieve
and array fields
Data Schemas & Data Modelling

Storing your Data Correctly


What’s Inside This Module?

Understanding Document Schemas &


Data Types

Modelling Relations

Schema Validation

You might also like