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

02 - Document-Based and MongoDB

The document provides information on document-based NoSQL systems and MongoDB. It discusses that document-based systems store data as collections of similar documents without predefined schemas. Documents can have different attributes. MongoDB is highlighted as an example of a document-based system. The data model of MongoDB is described as flexible and allowing embedded documents and arrays. MongoDB is designed to scale out across multiple servers and provide features like indexing, aggregation and file storage while maintaining performance. Basic concepts of MongoDB like documents, collections, databases and its dynamic schema are explained.

Uploaded by

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

02 - Document-Based and MongoDB

The document provides information on document-based NoSQL systems and MongoDB. It discusses that document-based systems store data as collections of similar documents without predefined schemas. Documents can have different attributes. MongoDB is highlighted as an example of a document-based system. The data model of MongoDB is described as flexible and allowing embedded documents and arrays. MongoDB is designed to scale out across multiple servers and provide features like indexing, aggregation and file storage while maintaining performance. Basic concepts of MongoDB like documents, collections, databases and its dynamic schema are explained.

Uploaded by

soso
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 133

Document-Based NOSQL

Systems and MongoDB


Document-Based Systems
 Document-based (document-oriented) NOSQL systems store data as a collection
of similar documents
 Documents resemble complex objects or XML documents
 The big difference is that documents are specified as self-describing data – no
schemas
 Documents can have different data elements (attributes)
 New documents can have attributes that do not exist in any of the documents in
the collection
 The system extracts the data element names from the self-describing documents
in the collection, and the user can request that the system creates indexes on
some of them
 Documents can be specified in different formats: XML, JSON
 MongoDB, CoutchDB are examples of document-based NOSQL systems
MongoDB Data Model

MongoDB is designed to work with documents without


any need of predefined columns
or data types (unlike relational databases), making the
data model extremely flexible.”
MongoDB – ease of use

 MongoDB is document-oriented – not a relational one


 The “row” concept is replaced with “document” (more
flexible)
 It allows embedded documents and arrays  complex
hierarchical relationships
 No predefined schemas
MongoDB – designed to scale

 Dataset sizes are growing in an incredible pace (more


bandwidth, cheap storage)
 Scaling a database
 Scaling up – getting a bigger machine
 Scaling out – partitioning data across more machines

 MongoDB is designed to scale out


 The document model makes it easy to split data across multiple servers

 It automatically
 Takes care of balancing data and load
 Redistributing documents
 Routing reads/writes to the correct machines
MongoDB – rich with features

 Aside from creating, reading, updating, and deleting data, it


provides many features you would expect
 Indexing
 Aggregation
 Special collection and index types
 Time-to-live collections
 Capped (fixed-size) collections

 File storage
 An easy-to-use protocol for storing large files and metadata
MongoDB – does not sacrifice speed

 Performance is a driving objective is MongoDB


 It uses opportunistic locking to maximize concurrency
 It uses as much RAM as it can as its cache
 It tries choose the correct indexes for queries
MongoDB basic concepts

 Document
 The basic unit of data
 Roughly equivalent to a row in RDBMS

 Collection
 Multiple documents compose a collection
 Can be thought as a table (but has a dynamic schema)

 Database
 Multiple collections compose a database

 Instance
 MongoDB instance can host multiple databases
MongoDB - documents

 A document is an ordered set of keys with associated values


 Representation varies among PLs – map, hash, dictionary
 It is type-sensitive and case-sensitive
 Every document has a special key called _id (unique)
{ {
field1:
Structure:
value1, _id: ObjectId("5099803df3f4948bd2f98391"),
field2: value2, name: { first: "Alan", last: "Turing" },
field3: value3, birth: new Date('Jun 23, 1912'),
... death: new Date('Jun 07, 1954'),
fieldN: valueN contribs: [ "Turing machine", "Turing test", "Turingery" ],
} views : NumberLong(1250000)
}
MongoDB - collections

 A collection is a group of documents


 MongoDB stores documents in collections
 Analogous to tables in relational DBs
MongoDB – dynamic schema
 Documents within the same collection can have the same or
different sets of fields or structure
 The following documents can be stored within a collection
 The second document has an additional field – country
 The R_ID field is a string in the first document and a string in the
second one
MongoDB – databases
 Collections are grouped into databases
 A single instance can host several databases
 Some reserved database names:
 admin : authentication and authorization
 Local: stores data specific to a single server, store data used in
replication process
 config: stores information about sharding
JSON and BSON
 MongoDB is a document-based database
 It uses BSON (Binary JSON) for storing data
 JSON stands for JavaScript Object Notation
 It is a standard used for data interchange in the Web
 It is a machine and human readable format
 All basic data types are supported by JSON
 BSON is a variation of JSON with additional data types, and
it is more efficient for storage than JSON
 MongoDB stores JSON documents in a binary-encoded
format (BSON)
JSON and BSON

JSON document
The Identifier _id
 Data is stored in documents
 Documents are made up of key-value pairs
 A key can be compared to a column in RDBMS
 A key is used for querying data from documents
 We also need a key to identify a document within a
collection – this is the _id identifier
 If you do not specify its value, MongoDB will do that for you
 This key is immutable
 It can be of any data type but arrays
Capped Collections
 This allows MongoDB to store documents in a collection in
their inserted order
 As the collection reaches its limit, documents will be
removed from the collection in FIFO
 Good for log files
Polymorphic Schema
 It is a schema where a collection has documents of different
types/schemas
 Perform queries on
common fields
 Perform queries on
specific fields
Installation and Configuration
 MongoDB is a cross-platform database
 A list of all available packages can be found on MongoDb
downloads page www.mongodb.org/downloads
 Choose type of deployment (local, community server)

 Download an msi file then run it


Running MongoDB
 Before running MongoDb a data folder is required for
storing files
 By default, this the c:\data\db
 This folder is not created by MongoDB
 You should create it with proper permissions
 Go to the bin folder and run mongod.exe (database server)

 Use the --dbpath if you want to specify a different folder


Running MongoDB
 After running mongod.exe, it will listen to connections from
the mongo shell (on port 27017 by default)
Running MongoDB
 You can safely stop mongod by typing Ctrl-C
MongoDB Shell - mongo.exe
 It comes as part of the standard distribution of MongoDB
 It provides a full database interface for MongoDB
 Once database services have started, you can run the
mongo shell
 Run mongo.exe from the bin folder
MongoDB Shell - mongosh
 The mongo shell is deprecated
 ‘mongosh‘ has improved usability
 Download it from
https://www.mongodb.com/try/download/shell?jmp=docs
 Run it from here
MongoDB Shell - mongosh
 On startup…
 The shell connects to the test database on a MongoDB server
 Assigns this database connection to the global variable db
Using the Shell
 The ‘db’ command shows the current database to which the
shell is connected to

 The ‘show dbs’ shows all databases

 The ‘use dbname’ switches to a different database or


creates one
Creating a Collection
 Syntax
db.createCollection(name, options)
parameters
Parameter Type Description
name String The name of the collection
options Document Specify memory and indexing options (optional)
options
Option Type Description
capped boolean If true it overwrites oldest entries when it
reaches its maximum size
size number Specifies maximum size in bytes (if capped is
true, then you have to set this option). Older
docs removed if size reached
max number Maximum number of documents if capped
autoindexid boolean If true, it creates an index for _id field
Creating a Collection
 Collections are created the first time they are referenced
 So the createCollection command is used to set some initial
parameters – capped.
Creating a Collection
 Example 1
db.createCollection(“mycollection”)

 Example 2
db.createCollection(“mycollection”, {capped: true, size:5234182, max:100})
The Company Example
Showing Collections
 Use ‘show collections’ to show collections of active database

 Example
MongoDB CRUD Operations
 Create/insert operations
 insertOne
 insertMany
 Read operations
 find
 Update operations
 updateOne
 updateMany
 replaceOne
 Delete operations
 deleteOne
 deleteMany
Insert a Single Document
 Syntax
db.collection.insertOne(doc,{writeconcern})

insertOne parameters
Parameter Type Description
doc document A doc to insert to a collection
writeConcern document The level of acknowledgment requested from
MongoDB for write operations

Return value
field Type Description
acknowledged boolean true: If the operation ran with write concern
insertedId string The _id of the inserted document
Insert a Single Document
 Behavior
 If the collection does not exist, it will be created
 If the _id field is not specified, it will be added
 On error, it throws an exception (writeError, writeConcernError)
 Atomicity
 All write operations are atomic on the level of a single document
Insert a Single Document

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

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


Insert a Single Document

db.products.insertOne({
_id: 10,
"item" : "packing peanuts",
"qty" : 200
})
Insert a Single Document

db.products.insertOne(
{ "item": "envelopes", "qty": 100, type: "Self-Sealing" },
{ writeConcern: { w : "majority", wtimeout : 100 } }
)
Insert Multiple Documents
 Syntax
db.collection.insertMany([doc1, doc2,…],{writeconcern, ordered})
parameters
Parameter Type Description
[doc1,doc2,…] Array of An array of documents to be inserted
documents
writeConcern document (optional) The level of acknowledgment
requested from MongoDB for write
operations
ordered boolean (optional) true if ordered insertion is
required (def. true).

insertMany returns document


field Type Description
acknowledged boolean true: If the operation ran with write concern
insertedId Array Array of _ids of successfully inserted documents
Insert Multiple Documents
db.products.insertMany( [
{ item: "card", qty: 15 },
{ item: "envelope", qty: 20 },
{ item: "stamps" , qty: 30 }
])

db.products.insertMany( [
{ _id: 10, item: "large box", qty: 20 },
{ _id: 11, item: "small box", qty: 55 },
{ _id: 12, item: "medium box", qty: 30 }
])
Insert Multiple Documents
db.products.insertMany( [ BulkWriteError({
"writeErrors" : [
{ _id: 13, item: "envelopes", qty: 60 }, {
{ _id: 13, item: "stamps", qty: 110 }, "index" : 0,
{ _id: 14, item: "packing tape", qty: 38 } "code" : 11000,
"errmsg" : "E11000 duplicate key error collection: inventory.products index: _id_ dup key:
]) { : 13.0 }",
"op" : {
"_id" : 13,
"item" : "stamps",
"qty" : 110
}
}
],
"writeConcernErrors" : [ ],
"nInserted" : 1,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
Insert Multiple Documents
db.products.insertMany( [
{ _id: 10, item: "large box", qty: 20 },
{ _id: 11, item: "small box", qty: 55 },
{ _id: 11, item: "medium box", qty: 30 },
{ _id: 12, item: "envelope", qty: 100},
{ _id: 13, item: "stamps", qty: 125 },
{ _id: 13, item: "tape", qty: 20},
{ _id: 14, item: "bubble wrap", qty: 30}
], { ordered: false } )
Insert Document(s)
 Syntax
db.collection.insert(doc or array of docs,{writeconcern, ordered})
The Company Example – option 1
The Company Example – option 2
 Worker references are embedded in the project document
The Company Example – option 3
 Workers are embedded in the project document.
 No need for the worker collection
Read Operations
 Read operations retrieve documents from a collection.
 MongoDB provides the following method for querying a collection
for documents
db.collection.find()
 You can specify query filters that identify the documents to return
Read Operations - querying
 Syntax
db.collection.find(query, projection)

find parameters
Parameter Type Description
query document - Specifies selection filter (optional)
- {} for all documents or leave empty
projection document - Specifies fields to return
- Default  all fields

 Returns a cursor to the returned documents


Read Operations - projections
 Determines which fields are returned in the matching documents
 Syntax
{field1: value, field2: value, . . .}

field name 0 or false for exclusion


1 or true for inclusion
 _id is included by default unless you specify _id:0
 A projection cannot contain both include and exclude
specifications (_id is an exception)
Read Operations - projections
 Fields in an embedded documents are specified using one of
these ways:
 Dot notation
 "field.nestedfield": <value>

 Nested form
{ field: { nestedfield: <value> } }
Read Operations – the bios collection

The bios example


Read Operations – specify fields to return
db.bios.find({ }, { name: 1, contribs: 1 })
Read Operations – specify fields to exclude
db.bios.find(
{ contribs: 'OOP' },
{ 'name.first': 0, birth: 0 }
)
Read Operations – explicitly exclude _id
Read Operations – array slicing and embedded documents

extracts the first two elements in contribs


Read Operations – specify values of projected fields

 You can
 Project new fields
 Project existing fields with new values

 It is done with
 Aggregation expressions
 Literals
 Aggregation variables
Read Operations – specify values of projected fields
Read Operations – specify values of projected fields
Read Operations – specify values of projected fields
Read Operations – specify values of projected fields
Read Operations – specify values of projected fields
Read Operations – aggregation expressions
Read Operations – aggregation expressions
db.bios.find(
{ },
{
_id: 0,
name: {
$concat: [
{ $ifNull: [ "$name.aka", "$name.first" ] },
" ",
"$name.last"
]
},
birth: 1,
contribs: 1,
awards: { $cond: { if: { $isArray: "$awards" }, then: { $size: "$awards" }, else: 0 } },

reportDate: { $dateToString: { date: new Date(), format: "%Y-%m-%d" } },


reportBy: "hellouser123",
reportNumber: { $literal: 1 }
}
)
Read Operations – aggregation expressions
Query – find all documents
Query – find all documents

db.inventory.insertMany([
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);

db.inventory.find( {} ) SELECT * FROM inventory


Query – equality

{ <field1>: <value1>, ... }

db.inventory.find( { status: "D" } )

SELECT * FROM inventory WHERE status = "D


Query – using operators
{ <field1>: { <operator1>: <value1> }, ... }

Uses the $in operator to return documents where _id equals either to 5 or ObjectId(…)
Query – using operators

{ <field1>: { <operator1>: <value1> }, ... }

db.inventory.find( { status: { $in: [ "A", "D" ] } } )

SELECT * FROM inventory WHERE status in ("A", "D")


Query – comparison operators

Name Description
$eq Matches values that are equal to a specified value.
$gt Matches values that are greater than a specified
value.

$gte Matches values that are greater than or equal to a


specified value.

$in Matches any of the values specified in an array.


$lt Matches values that are less than a specified value.
$lte Matches values that are less than or equal to a
specified value.

$ne Matches all values that are not equal to a specified


value.

$nin Matches none of the values specified in an array.


Query – logical operators

Name Description
$and Joins query clauses with a logical AND returns all
documents that match the conditions of both clauses.
$not Inverts the effect of a query expression and returns
documents that do not match the query expression.
$nor Joins query clauses with a logical NOR returns all
documents that fail to match both clauses.
$or Joins query clauses with a logical OR returns all
documents that match the conditions of either
clause.
Query – logical operators

Name Description
$and Joins query clauses with a logical AND returns all
documents that match the conditions of both clauses.
$not Inverts the effect of a query expression and returns
documents that do not match the query expression.
$nor Joins query clauses with a logical NOR returns all
documents that fail to match both clauses.
$or Joins query clauses with a logical OR returns all
documents that match the conditions of either
clause.
Query – greater than operator
Query – AND operator

db.inventory.find( { status: "A", qty: { $lt: 30 } } )

SELECT * FROM inventory WHERE status = "A" AND qty < 30


Query – multiple conditions

returns all documents where birth


is greater than the specified date
and death field does not exist
Query – OR operator

db.inventory.find(
{ $or: [ { status: "A" }, { qty: { $lt: 30 } } ] }
)

SELECT * FROM inventory WHERE status = "A" OR qty < 30


Query – AND and OR operators

db.inventory.find( {
status: "A",
$or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
})

SELECT * FROM inventory


WHERE status = "A" AND ( qty < 30 OR item
LIKE "p%")
Query – regular expression operator
Query – ranges
Query – exact matches on embedded documents

query does not match these documents


Query – fields of an embedded documents

query does match these documents


Match an array

 Specifying equality condition on an array

{ <field>: <value> }

 Where
 <field> is the name of the array
 <value> is the exact array to match
Match an array

db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },
{ item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },
{ item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },
{ item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },
{ item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
])

 queries for all documents where the tags is an array with exactly two
elements in the specified order:

db.inventory.find( { tags: ["red", "blank"] } )

 find an array that contains both the elements without regard to order or other
elements in the array

db.inventory.find( { tags: { $all: ["red", "blank"] } } )


Query an array for an element

 Returns documents where the contribs contains the element “UNIX”

db.inventory.find( { tags: "red" } )


Query an array for an element

 To specify conditions on the elements of the array


{ <array field>: { <operator1>: <value1>, ... } }

db.inventory.find( { dim_cm: { $gt: 25 } } )

 Returns documents where the contribs contains the element


“ALGOL” or “Lisp”

 Returns documents where the contribs array size is 4


Query an array elements with compound conditions

 Conditions are satisfied somehow – one element satisfies the first


and another satisfies the second
db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )

 Looks for one element at least that satisfies all conditions

db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )

 Looks for documents where the second element > 25

db.inventory.find( { "dim_cm.1": { $gt: 25 } } )


Query – array of documents

db.inventory.insertMany( [
{ item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
{ item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },
{ item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },
{ item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
Query for a document nested in an array

 Returns documents where the awards array contains an element with


award=“turing award”

 Selects all documents where an element in the instock array matches the
specified document
db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } )
Query on a field embedded in the array of documents

 Returns documents where the awards array contains an element with


award=“turing award”

 Selects all documents where the instock array has at least one
embedded document that contains the field qty whose value is less
than or equal to 20
db.inventory.find( { 'instock.qty': { $lte: 20 } } )
Query on a field embedded in the array of documents

 Selects all documents where the instock array has as its first element a
document that contains the field qty whose value is less than or equal to
20

db.inventory.find( { 'instock.0.qty': { $lte: 20 } } )


Multiple conditions on array of documents

 Returns documents where the awards array contains at least one element
with award=“turing award” and year > 1980

 Selects documents where the instock array has at least one embedded
document that contains the qty equal to 5 and warehouse equal to A:
db.inventory.find(
{ "instock": { $elemMatch: { qty: 5, warehouse: "A" } } }
)

 queries for documents where the instock array has at least one embedded
document that contains qty that is greater than 10 and less than or equal to 20

db.inventory.find(
{ "instock": { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } } )
The Query Returned Cursor

 The find() command returns a cursor to the results


 If it is not assigned to a variable, then it is iterated automatically
for the first 20 documents
db.collection.find()
 This is why we receive results on screen automatically
The Query Returned Cursor – assign to a variable
The Query Returned Cursor Methods

 The next() method gives the next document


 The hasNext() method returns true if the cursor can iterate
further to return more document
 You can use the forEach() method to iterate the cursor
The Query Returned Cursor Methods
The Query Returned Cursor Methods
Order Documents in the Result Set

 sorts the documents first by the age field in descending order and then by the posts
field in ascending order
db.users.find({ }).sort( { age : -1, posts: 1 } )
Limit the Number of Documents to Return
Update Operations
 Read operations modify documents in a collection
 MongoDB provides the following methods for updating
 updateOne
 updateMany
 replaceOne

 Update operations target a single collection


 All write operation are atomic on the level of a single
document
Update a Single Document
 Syntax
db.collection.updateOne(filter, update, options)
updateOne parameters
Parameter Type Description
filter document - The selection criteria (as in find())
- {} updates the first document returned
update document or The modifications to apply
pipeline
options document document fields: upsert, writeConcern, collation,
arrayFilters, hint

updateOne returns a document


field Type Description
matchedCount number # matched documents
modifiedCount number # modified documents
upsertedId String _id of the upserted document
Update a Single Document
 Syntax
db.collection.updateOne(filter, update, options)
updateOne options parameter
Parameter Type Description
upsert boolean - true  update or insert if there is no match
- false (default)  does not insert if no match is
found
writeConcern document The modifications to apply
arrayFilters array An array that determines which array elements to
modify on an array fields
Update Operators
 To update a document use update operators:

{
operator1: {field1: value1},
operator2: {field2: value2},

}
Update Operators – fields
Field operators
name format Description
$inc { $inc: { <f1>: <v1>, ... } } f1 = f1 + v1
$min { $min: { <f1>: <v1>, ... } } if v1 < f1 then f1 = v1
$max { $max: { <f1>: <v1>, ... } } if v1 > f1 then f1 = v1
$set { $set: { <f1>: <v1>, ... } } f1 = v1
$mul { $mul: { <f1>: <v1>, ... } } f1 = f1 * v1
$unset { $unset: { <f1>: “”, ... } } deletes f1 from the document
$rename { $inc: { <f1>: <newname>, ... } } renames a field
Update Operators – fields

{ "_id" : 1, "name" : "Central Perk Cafe", "Borough" : "Manhattan" },


{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "Borough" : "Queens", "violations" :
2 },
{ "_id" : 3, "name" : "Empire State Pub", "Borough" : "Brooklyn", "violations" : 0 }

db.restaurant.updateOne(
{ "name" : "Central Perk Cafe" },
{ $set: { "violations" : 3 } }
)
Update Operators – arrays – $ operator
 Refers to the first array element that matches the update filter
parameter
 Syntax
db.collection.updateOne(
{ <array>: value ... },
{ <update operator>: { "<array>.$" : value } }
)

 The <array> field MUST appear in the filter part


Update Operators – arrays – $ operator

Values in an array

db.students.insert([
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
])

db.students.updateOne(
{ _id: 1, grades: 80 },
{ $set: { "grades.$" : 82 } }
)
Update Operators – arrays – $ operator

Documents in an array
{
_id: 4,
grades: [
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 85, mean: 85, std: 8 }
]
}

db.students.updateOne(
{ _id: 4, "grades.grade": 85 },
{ $set: { "grades.$.std" : 6 } }
)
Update Operators – arrays – $ operator
Embedded Documents Using Multiple Field Matches
{
_id: 4,
grades: [
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 85, mean: 85, std: 8 }
]
}
db.students.updateOne(
{
_id: 4,
grades: { $elemMatch: { grade: { $lte: 90 }, mean: { $gt: 80 } } }
},
{ $set: { "grades.$.std" : 6 } }
)
Update Operators – arrays – $[ ] operator
 updates all array elements in the specified array that matches the
query
 Syntax
db.collection.updateMany(
{ <query> },
{ <update operator>: { "<array>.$[ ]" : value } }
)
Update Operators – arrays – $[ ] operator

Update all elements in an array

db.students.insert([
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
])

db.students.update(
{ },
{ $inc: { "grades.$[]": 10 } },
{ multi: true }
)
Update Operators – arrays – $[ ] operator

Update all documents in an array


{ db.students2.update(
"_id" : 1, { },
"grades" : [ { $inc: { "grades.$[].std" : -2 } },
{ "grade" : 80, "mean" : 75, "std" : 8 },
{ multi: true }
{ "grade" : 85, "mean" : 90, "std" : 6 },
)
{ "grade" : 85, "mean" : 85, "std" : 8 } ]
}
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 75, "std" : 8 },
{ "grade" : 87, "mean" : 90, "std" : 5 },
{ "grade" : 85, "mean" : 85, "std" : 6 } ]
}
Update Operators – arrays – $[ ] operator

Update arrays using negation operator

{ "_id" : 1, "grades" : [ 85, 82, 80 ] }


{ "_id" : 2, "grades" : [ 88, 90, 92 ] }
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }

db.results.updateMany(
{ "grades" : { $ne: 100 } },
{ $inc: { "grades.$[]": 10 } },
{ multi: true }
)

{ "_id" : 1, "grades" : [ 95, 92, 90 ] }


{ "_id" : 2, "grades" : [ 98, 100, 102 ] }
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
Update Operators – arrays – $[identifier] operator
 Refers to all array elements that match the update arrayFilters
 Syntax

db.collection.updateMany(
{ <query conditions> },
{ <update operator>: { "<array>.$[<identifier>]" : value } },
{ arrayFilters: [ { <identifier>: <condition> } ] }
)
Update Operators – arrays – $[identifier] operator
Update array elements that match arrayFilters
{ "_id" : 1, "grades" : [ 95, 92, 90 ] }
{ "_id" : 2, "grades" : [ 98, 100, 102 ] }
{ "_id" : 3, "grades" : [ 95, 110, 100 ] }

db.students.update(
{ },
{ $set: { "grades.$[element]" : 100 } },
{ multi: true,
arrayFilters: [ { "element": { $gte: 100 } } ]
}
)

{ "_id" : 1, "grades" : [ 95, 92, 90 ] }


{ "_id" : 2, "grades" : [ 98, 100, 100 ] }
{ "_id" : 3, "grades" : [ 95, 100, 100 ] }
Update Operators – arrays – $[identifier] operator
Update array documents that match arrayFilters
{ db.students2.update(
"_id" : 1,
{ },
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 6 }, { $set: { "grades.$[elem].mean" : 100 } },
{ "grade" : 85, "mean" : 90, "std" : 4 }, {
{ "grade" : 85, "mean" : 85, "std" : 6 }
]
multi: true,
} arrayFilters: [ { "elem.grade": { $gte: 85 } } ]
{ }
"_id" : 2,
"grades" : [
)
{ "grade" : 90, "mean" : 75, "std" : 6 },
{ "grade" : 87, "mean" : 90, "std" : 3 },
{ "grade" : 85, "mean" : 85, "std" : 4 }
]
}
Update Operators – arrays – $[identifier] operator
Update array elements that match multiple conditions
{ db.students2.update(
"_id" : 1,
{ },
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 6 }, { $inc: { "grades.$[elem].std" : -1 } },
{ "grade" : 85, "mean" : 90, "std" : 4 }, { arrayFilters: [ { "elem.grade": { $gte: 80 },
{ "grade" : 85, "mean" : 85, "std" : 6 } "elem.std": { $gt: 5 } } ], multi: true }
]
}
)
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 75, "std" : 6 },
{ "grade" : 87, "mean" : 90, "std" : 3 },
{ "grade" : 85, "mean" : 85, "std" : 4 }
]
}
Update Operators – arrays – $[identifier] operator
Update array elements using negation
{
"_id": 1, db.alumni.update(
"name": "Christine Franklin",
"degrees": [ { },
{ "level": "Master",
"major": "Biology", { $set : { "degrees.$[degree].gradcampaign" : 1 } },
"completion_year": 2010,
"faculty": "Science" { arrayFilters : [ {"degree.level" : { $ne: "Bachelor" } } ],
},
{ multi : true }
"level": "Bachelor",
"major": "Biology", )
"completion_year": 2008,
"faculty": "Science"
}
],
"school_email": "[email protected]",
"email": "[email protected]"
}
{
"_id": 2,
"name": "Reyansh Sengupta",
"degrees": [
{ "level": "Bachelor",
"major": "Chemical Engineering",
"completion_year": 2002,
"faculty": "Engineering"
}
],
"school_email": "[email protected]"
}
Update Operators – arrays – $[ ] & $[idf]
operators
Update nested arrays
db.students3.insert([
{ "_id" : 1,
"grades" : [
{ type: "quiz", questions: [ 10, 8, 5 ] },
{ type: "quiz", questions: [ 8, 9, 6 ] },
{ type: "hw", questions: [ 5, 4, 3 ] },
{ type: "exam", questions: [ 25, 10, 23, 0 ] } ]
}
])
db.students3.update(
{},
{ $inc: { "grades.$[].questions.$[score]": 2 } },
{ arrayFilters: [ { "score": { $gte: 8 } } ], multi: true}
)
Update Operators – arrays – $[ ] & $[idf]
operators
Update nested arrays
db.students3.insert([
{ "_id" : 1,
"grades" : [
{ type: "quiz", questions: [ 10, 8, 5 ] },
{ type: "quiz", questions: [ 8, 9, 6 ] },
{ type: "hw", questions: [ 5, 4, 3 ] },
{ type: "exam", questions: [ 25, 10, 23, 0 ] } ]
}
])
db.students3.update(
{},
{ $inc: { "grades.$[t].questions.$[score]": 2 } },
{ arrayFilters: [ { "t.type": "quiz" } , { "score": { $gte: 8 } } ], multi: true}
)
Update Operators – arrays – more operators

Name Description

$addToSet Adds elements to an array only if they do not already exist


in the set.

$pop Removes the first or last item of an array.

$pull Removes all array elements that match a specified query.

$push Adds an item to an array.

$pullAll Removes all matching values from an array.


Update Operators – arrays – modifiers
 Modifier are operators that change a bit the behavior of other
operators

Name Description
$each Modifies the $push and $addToSet operators to
append multiple items for array updates.
$position Modifies the $push operator to specify the position in
the array to add elements.
$slice Modifies the $push operator to limit the size of
updated arrays.
$sort Modifies the $push operator to reorder documents
stored in an array.
Update Operators – arrays – modifiers
db.students.update(
{ name: "joe" },
{ $push: { scores: { $each: [ 90, 92, 85 ] } } }
)

db.inventory.update(
{ _id: 2 },
{ $addToSet: { tags: { $each: [ "camera", "electronics",
"accessories" ] } } }
)
Update with an Aggregation Pipeline
 Aggregation operations process data records and return computed
results
 They group values from multiple documents
 Perform a variety of operations on the grouped data
 Return a single result
 MongoDb provides three ways to perform aggregations:
 Aggregation pipeline
 Map-reduce function
 Single purpose aggregation methods
Aggregation Pipeline
 MongoDB’s aggregation framework is modeled on the concept of
data processing pipelines
 Aggregation pipeline consists of stages
 Each stage transforms the documents as they pass through the pipeline
 Stages do not need to produce one output document for every input
document
 Some stages may generate new documents and others may filter out
documents
 MongoDB provides the aggregate() shell method to run aggregate
pipeline
 It can be applied for updates
Using Aggregation Pipeline in Update
 Update method syntax
db.collection.updateOne(filter, aggregation pipeline, options)
db.collection.updateOne(filter, [stage1, stage2,…], options)

 Allowed aggregation stages


 $addfields – adds new fields to documents
 $set – it is an alias for $addfields
 $project – passes along the documents with the requested fields
(existing & newly computed) to the next stage
 $unset – it is an alias for $project
 $replaceRoot – replaces the input document with the specified one
 $replaceWith - it is an alias for $replaceRoot
Using Aggregation Pipeline in Update
db.members.insertMany([
{ "_id" : 1, "member" : "abc123", "status" : "A", "points" : 2, "misc1" : "note to self: confirm
status", "misc2" : "Need to activate", "lastUpdate" : ISODate("2019-01-01T00:00:00Z") },

{ "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, comments: [ "reminder: ping me
at 100pts", "Some random comment" ], "lastUpdate" : ISODate("2019-01-01T00:00:00Z") }

])

db.members.updateOne(
{ _id: 1 },
[
{ $set: { status: "Modified", comments: [ "$misc1", "$misc2" ], lastUpdate: "$$NOW" } },
{ $unset: [ "misc1", "misc2" ] }
]
)
Using Aggregation Pipeline in Update

 The third document _id: 3 is missing the average and grade fields. Using an
aggregation pipeline, you can update the document with the calculated grade
average and letter grade.

db.students3.insert([
{ "_id" : 1, "tests" : [ 95, 92, 90 ], "average" : 92, "grade" : "A", "lastUpdate" : ISODate("2020-01-
23T05:18:40.013Z") },
{ "_id" : 2, "tests" : [ 94, 88, 90 ], "average" : 91, "grade" : "A", "lastUpdate" : ISODate("2020-01-
23T05:18:40.013Z") },
{ "_id" : 3, "tests" : [ 70, 75, 82 ], "lastUpdate" : ISODate("2019-01-01T00:00:00Z") }
]);
Using Aggregation Pipeline in Update
 The third document _id: 3 is missing the average and grade fields. Using an
aggregation pipeline, you can update the document with the calculated grade
average and letter grade.
db.students3.updateOne(
{ _id: 3 },
[
{ $set: { average: { $trunc: [ { $avg: "$tests" }, 0 ] }, lastUpdate: "$$NOW" } },

{ $set: { grade: { $switch: {


branches: [
{ case: { $gte: [ "$average", 90 ] }, then: "A" },
{ case: { $gte: [ "$average", 80 ] }, then: "B" },
{ case: { $gte: [ "$average", 70 ] }, then: "C" },
{ case: { $gte: [ "$average", 60 ] }, then: "D" }
],
default: "F"
}}}}
]
)
Replace a Single Document
 Replaces the first matching document that matches the filter
 Syntax
db.collection.replaceOne(filter, replacement, options)
updateOne parameters
Parameter Type Description
filter document - The selection criteria (as in find())
- {} updates the first document returned
replacement document The replacement document
options document document fields: upsert, writeConcern, collation,
arrayFilters, hint

updateOne returns a document


field Type Description
matchedCount number # matched documents
modifiedCount number # modified documents
upsertedId String _id of the upserted document
Replace a Single Document
 Replaces a single document where name: “Central Perk Café”

{ "_id" : 1, "name" : "Central Perk Cafe", "Borough" : "Manhattan" },


{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "Borough" : "Queens", "violations" : 2 },
{ "_id" : 3, "name" : "Empire State Pub", "Borough" : "Brooklyn", "violations" : 0 }

db.restaurant.replaceOne(
{ "name" : "Central Perk Cafe" },
{ "name" : "Central Pork Cafe", "Borough" : "Manhattan" }
)
Replace a Single Document
 Attempts to replace the document with name: “Pizza Rat’s Pizzaria” with
upsert : true
{ "_id" : 1, "name" : "Central Perk Cafe", "Borough" : "Manhattan", "violations" : 3 },
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "Borough" : "Queens", "violations" : 2 },
{ "_id" : 3, "name" : "Empire State Pub", "Borough" : "Brooklyn", "violations" : 0 }

db.restaurant.replaceOne(
{ "name" : "Pizza Rat's Pizzaria" },
{ "_id": 4, "name" : "Pizza Rat's Pizzaria", "Borough" : "Manhattan", "violations" : 8 },
{ upsert: true }
)
Delete Operations
 Delete operations remove documents from a collection
 MongoDB provides the following methods for updating
 deleteOne
 deleteMany
 delete operations target a single collection
 All write operation are atomic on the level of a single
document
Delete a Single Document
 Syntax
db.collection.deleteOne(filter, options)
deleteOne options parameter
field Type Description
writeConcern document A document expressing the write concern
collaction document Allows users to specify language-specific rules for
string comparison (lettercase…)

deleteOne returns a document


field Type Description
acknowledged boolean If the operation ran with write concern
deletedCount number # deleted documents
Delete a Single Document
 Deletes the order with _id : ObjectId(“56563237a41a4d68582c2509da”)
{
_id: ObjectId("563237a41a4d68582c2509da"),
stock: "Brent Crude Futures",
qty: 250,
type: "buy-limit",
limit: 48.90,
creationts: ISODate("2015-11-01T12:30:15Z"),
expiryts: ISODate("2015-11-01T12:35:15Z"),
client: "Crude Traders Inc."
}

db.orders.deleteOne( { "_id" :
ObjectId("563237a41a4d68582c2509da") } )
Delete a Single Document
 Deletes the first document with expiryts > ISODate("2015-11-01T12:40:15Z"
{
_id: ObjectId("563237a41a4d68582c2509da"),
stock: "Brent Crude Futures",
qty: 250,
type: "buy-limit",
limit: 48.90,
creationts: ISODate("2015-11-01T12:30:15Z"),
expiryts: ISODate("2015-11-01T12:35:15Z"),
client: "Crude Traders Inc."
}

db.orders.deleteOne( { "expiryts" : { $lt:


ISODate("2015-11-01T12:40:15Z") } } )

You might also like