0% found this document useful (0 votes)
32 views23 pages

DBS UNIT V Notes

This document provides an overview of MongoDB, including its key concepts, features, and operations. It describes MongoDB's use of databases and collections analogous to tables in relational databases. Documents are the basic data structure and can have dynamic schemas. The document also outlines MongoDB's core CRUD operations for creating, reading, updating, and deleting documents, as well as its support for aggregation, map-reduce functions, and high availability through replication.

Uploaded by

yogeshsaini5364
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)
32 views23 pages

DBS UNIT V Notes

This document provides an overview of MongoDB, including its key concepts, features, and operations. It describes MongoDB's use of databases and collections analogous to tables in relational databases. Documents are the basic data structure and can have dynamic schemas. The document also outlines MongoDB's core CRUD operations for creating, reading, updating, and deleting documents, as well as its support for aggregation, map-reduce functions, and high availability through replication.

Uploaded by

yogeshsaini5364
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/ 23

Nithya BN

Assistant Professor
Department of Computer Applications
M S Ramaiah Institute of Technology
[email protected]
Ph:9900087291

MCA23: Database Systems: Unit V


Database Systems - Unit V Nithya BN

Contents
1 MongoDB 2
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Key Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Concepts of MongoDB . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5.1 Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.6 Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.7 DataTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.7.1 Basic DataTypes . . . . . . . . . . . . . . . . . . . . . . . 7
1.8 CRUD Operations . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.8.1 Create Operation . . . . . . . . . . . . . . . . . . . . . . . 8
1.8.2 Query Documents . . . . . . . . . . . . . . . . . . . . . . 11
1.8.3 Update Operation . . . . . . . . . . . . . . . . . . . . . . 14
1.8.4 Delete Operation . . . . . . . . . . . . . . . . . . . . . . . 15
1.8.5 Bulk Write Operations . . . . . . . . . . . . . . . . . . . . 16
1.9 Aggregation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.10 Map-Reduce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.10.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Page 1
Database Systems - Unit V Nithya BN

UNIT V
MongoDB
Introduction: Features, Database, Collection, Documents, Data Types
CRUD Operations: Create, Read, Update, Delete, Bulk Write
Aggregation: Aggregation Pipeline, Map-Reduce, Single Purpose Aggregation
Operations

1 MongoDB
1.1 Introduction
MongoDB is a document database designed for ease of development and scaling.
The Manual introduces key concepts in MongoDB, presents the query language,
and provides operational and administrative considerations and procedures as
well as a comprehensive reference section.MongoDB offers both a Community
and an Enterprise version of the database.

MongoDB Community is the source available and free to use edition of Mon-
goDB. MongoDB Enterprise is available as part of the MongoDB Enterprise
Advanced subscription and includes comprehensive support for your MongoDB
deployment. MongoDB Enterprise also adds enterprise-focused features such as
LDAP and Kerberos support, on-disk encryption, and auditing.

Document Database
A record in MongoDB is a document, which is a data structure composed of
field and value pairs. MongoDB documents are similar to JSON objects. The
values of fields may include other documents, arrays, and arrays of documents.

The advantages of using documents are:


• Documents (i.e. objects) correspond to native data types in many pro-
gramming languages.
• Embedded documents and arrays reduce need for expensive joins.
• Dynamic schema supports fluent polymorphism.
Collections/Views/On-Demand Materialized Views
MongoDB stores documents in collections. Collections are analogous to tables
in relational databases.

Page 2
Database Systems - Unit V Nithya BN

1.2 Key Features


High Performance MongoDB provides high performance data persistence. In
particular,
• Support for embedded data models reduces I/O activity on database sys-
tem.
• Indexes support faster queries and can include keys from embedded doc-
uments and arrays.
Rich Query Language MongoDB supports a rich query language to support
read and write operations (CRUD) as well as:
• Data Aggregation
• Text Search and Geospatial Queries.
High Availability MongoDB’s replication facility, called replica set, provides:
• automatic failover
• data redundancy.
A replica set is a group of MongoDB servers that maintain the same data set,
providing redundancy and increasing data availability.

Horizontal Scalability MongoDB provides horizontal scalability as part of


its core functionality:
• Sharding distributes data across a cluster of machines.
• Starting in 3.4, MongoDB supports creating zones of data based on the
shard key. In a balanced cluster, MongoDB directs reads and writes cov-
ered by a zone only to those shards inside the zone. See the Zones manual
page for more information.
Support for Multiple Storage Engines MongoDB supports multiple storage
engines:
• WiredTiger Storage Engine (including support for Encryption at Rest)
• In-Memory Storage Engine.
In addition, MongoDB provides pluggable storage engine API that allows third
parties to develop storage engines for MongoDB.

1.3 Concepts of MongoDB


• A document is the basic unit of data for MongoDB and is roughly equiva-
lent to a row in a relational database management system (but much more
expressive)
• Similarly, a collection can be thought of as a table with a dynamic schema
• A single instance of MongoDB can host multiple independent databases,
each of which contains its own collections.

Page 3
Database Systems - Unit V Nithya BN

• Every document has a special key, ” id”, that is unique within a collection
• MongoDB is distributed with a simple but powerful tool called the mongo
shell. The mongo shell provides built - in support for administering Mon-
goDB instances and manipulating data using the MongoDB query lan-
guage. It is also a fully functional JavaScript interpreter that enables
users to create and load their own scripts for a variety of purposes.

1.4 Databases
MongoDB groups collections into databases. A single instance of MongoDB can
host several databases, each grouping together zero or more collections. A good
rule of thumb is to store all data fro a single application in the same database.
seperate databases are userful when storing data for several applications or users
on the same MongoDB server.
Databases are identified by name. Database names can be any UTF - 8 string,
with the following restrictions:
• The empty string (””) is not a valid database name.
• A database name cannot contain any of these characters:/, \, ., ”, ∗, <, >
, :, |, ?, $(a single space), or \0(the null character). Basically, stick with
alphanumeric ASCII.
• Database names are case - insensitive.
• Database names are limited to a maximum of 64 bytes.
There are also some reserved database names, which you can access but which
have special semantics. These are as follows:

admin: The admin database plays a role in authentication and authorization.

local: This database stores data specific to a single server. In replica sets,
local stores data used in the replication process. The local database itself is
never replicated.

config Sharded MongoDB clusters use the config database to store informa-
tion about each shard.

By concatenating a database name with a collection in that database you can


get a fully qualified collection name, which is called a namespace.

1.5 Collection
Acollection is a group of documents. If a document is the MongoDB analog of
a row in a relational database, then a collection can be thought of as the analog
of a table.
Dynamic Schemas
Collections have dynamic schemas. This means that the documents within a
single collection can have any number of different ”shapes”. For example, both
of the following documents could be stored in a single collection:

Page 4
Database Systems - Unit V Nithya BN

”greetings”: ”Hello,World!”, ”views”:3 ”signoff”: ”Goodnight, and good luck”


Note that the previous documents have different keys, different numbers of keys,
and values of different types. Good reasons to keep the documents separate from
each other are listed below:
• Keeping different kinds of documents in the same collection can be a night-
mare for developers and admins. Developers need to make sure that each
query is only returning documents adhering to a particular schema or that
the application code performing a query can handle documents of differ-
ent shapes. If we are querying for blog posts, it’s a hassle to weed out
documents containing author data.
• It is much faster to get a list of collection than to extract a list of the types
of documents in a collection. For example, if we had a ”type” field in each
document that specified whether the document was a”skim”, ”whole”, or
”chunky monkey”, it would be much slower to find those three values in
a single collection that to have three separate collections and query the
correct collection.
• Grouping documents of the same kind together in the same collection
allows for data locality. Getting several blog posts from a collection con-
taining only posts will likely require fewer disk seeks than getting the same
posts from a collection containing posts and author data.
• We begin to impose some structure on our documents when we create
indexes. (This is especially true in the case of unique indexes.) These
indexes are defined per collection. By putting only documents of a single
type into the same collection, we can index our collections more efficiently.

1.5.1 Naming
A collection is identified by its name. Collection names have a few restrictions
listed below.
• The empty string(””)is not a valid collection name.
• Collection names may not contain the character, especially the null char-
acter, because this delineates the end of a collection name.

• You should not create any collections with names that start with system.,
a prefix reserved for internal collections. For example, the system.users
collection contains the databases users and the system.name spaces col-
lection contains information about all of the databases collection.

• User Created collections should not contain the reserved character $ in


their names. The various drivers available for the database do support
using $ in collection names because some system-generated collections
contain it, but you should not use $ in a name unless you are accessing
one of these collections.

Page 5
Database Systems - Unit V Nithya BN

Subcollection One convention for organizing collections is to use names-


paced subcolletions separated by the .character. For example, an appli-
cation containing a blog might have a collection named blog.posts and
a separate collection named blog.authors. This is for organisational pur-
poses only - there is no relationship between the blog collection (it doesnt
eve have to exist) and its ”children”.
Although subcollections do not have any special properties, they are useful
and are incorporated into many MongoDB tools.For instance:

– GridFS, a protocol for storing large files, uses subcollections to store


file metadata separately from content chunks.
– Most drvers provide some syntactic sugar for accessing a subcollec-
tion of a given collection. For example, in the database shell, db.blog
will give you the blog collection, and db.blog.posts will give you the
blog.posts collection.
Subcollections are a good way to organize data in MongoDB for many use
cases.

1.6 Documents
At the heart of MongoDB is the document: an ordered set of keys with associ-
ated values. The representation of a document varies by programming language,
but most languages have a data structure that is a natural fit, such as a map,
hash or dictionary. In JavaScript, for example, documents are represented as
objects:

{”greetings” : ”Hello, World!”}

This simple document contains a single key, ”greeting”, with a value of ”Hello,
world!”. Most documents will be more complex than this simple one and often
wll contain multiple key/value pairs:

{”greetings” : ”Hello, world!”, ”Views” : 3}

As you can see, values in documents are not just ”blobs”. They can be one
of several different datatypes (or even an entire embedded document. In this
example the value for ”greetings” is a string, whereas the value for ”views” is
an integer.
The keys in a document are strings.
• Keys must not contain the character of null. This character is used to
signify the end of a key.
• The. and $ characters have some special properties and should be used
only in certain circumstances. In general, they should be considered re-
served, and drivers will complain if they are used inappropriately.
• MongoDB is type sensitive and case sensitive.
• MongoDB cannot contain duplicate keys.

Page 6
Database Systems - Unit V Nithya BN

1.7 DataTypes
1.7.1 Basic DataTypes
• Null
The null type can be used to represent both a null value and a nonexistent
field:

{”x” : null}

• Boolean
There is a boolean type, which can be used for the values true and false.

{”x” : true}
• Number
The shell defaults to using 64 - bit floating point numbers. thus, these
numbers both look ”normal” in the shell:

{”x”:3.14}
{”x” :3}
For integers, use the NumberInt or NumberLong classes, which represent
4 - byte or 8-byte signed integers, respectively.

{”x” : NumberInt(”3”)}
{”x” : NumberLong(”3”)}
• String
Any string of UTF -8 characters can be represented using the string type:

{”x”:”foobar”}
• Date
MongoDB stores dates as 64-bit integers representing milliseconds since
the Unix Epoch (January 1, 1970). The time zone is not stored:

{”x” :new Date()}


• Regular Expression
Queries can use regular expression using JavaScripts regular expression
syntax”
{”x” :/foobar/i}
• Array
Sets or lists of values can be represented as arrays:

{”x” : [”a”, ”b”, ”c”]}


• Embedded document
Documents can contain entire documents embedded as values in a parent
document

{”x” : {”f oo” : ”bar”}}

Page 7
Database Systems - Unit V Nithya BN

• Object ID
An Object ID is a 12 - byte ID for documents:

{”x” : ObjectId ()}


• Binary data
Binary data is a string of arbitrary bytes. It cannot be manipulated from
the shell, Binary data is the only way to save non-UTF-8 strings to the
database.

• Code
MondoDB also makes it possible to store arbitrary JavaScript in queries
and documents:

{”x” : f unction () {/ ∗ · · · ∗ /}}

1.8 CRUD Operations


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

1.8.1 Create Operation


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:

• db.collection.insertOne()
• db.collection.insertMany()
In MongoDB, insert operations target a single collection. All write operations
in MongoDB are atomic on the level of a single document.

Create and Insert examples The following example inserts a new document
into the inventory collection. If the document does not specify an id field,
MongoDB adds the id field with an ObjectId value to the new document.

Page 8
Database Systems - Unit V Nithya BN

To retrieve the document that you just inserted, query the collection:

Insert Multiple Documents db.collection.insertMany() can insert multiple


documents into a collection. Pass an array of documents to the method.
The following example inserts three new documents into the inventory collection.
If the documents do not specify an id field, MongoDB adds the id field with
an ObjectId value to each document.

Page 9
Database Systems - Unit V Nithya BN

insertMany() returns a document that includes the newly inserted documents


id field values.
To retrieve the inserted documents, query the collection:

Insert Behavior
Collection Creation
If the collection does not currently exist, insert operations will create the col-
lection.
id Field
In MongoDB, each document stored in a collection requires a unique id field
that acts as a primary key. If an inserted document omits the id field, the
MongoDB driver automatically generates an ObjectId for the id field.
This also applies to documents inserted through update operations with upsert:
true.
Atomicity
All write operations in MongoDB are atomic on the level of a single document.

Insert Methods MongoDB provides the following methods for inserting doc-
uments into a collection:
db.collection.insertOne(): Inserts a single document into a collection.
db.collection.insertMany(): inserts multiple documents into a collection.
db.collection.insert() inserts a single document or multiple documents into a
collection.

Additional Methods for Inserts


• db.collection.update() when used with the upsert: true option.
• db.collection.updateOne() when used with the upsert: true option.
• db.collection.updateMany() when used with the upsert: true option.
• db.collection.findAndModify() when used with the upsert: true option.

Page 10
Database Systems - Unit V Nithya BN

• db.collection.findOneAndUpdate() when used with the upsert: true op-


tion.
• db.collection.findOneAndReplace() when used with the upsert: true op-
tion.
• db.collection.bulkWrite().

1.8.2 Query Documents


The examples on this page use the inventory collection. Populate the inventory
collection with the following documents:

Select All Documents in a Collection To select all documents in the


collection, pass an empty document as the query filter parameter to the query
bar. The query filter parameter determines the select criteria:
db.inventory.find( )

Specify Equality Condition To specify equality conditions,


use hf ieldi : hvaluei
expressions in the query filter document:
{hf ield1i : hvalue1, i · · · }
The following example selects from the inventory collection all documents where
the status equals ”D”:
db.inventory.f ind ({status : ”D”})
This operation corresponds to the following SQL statement:
SELECT * FROM inventory WHERE status = ”D”

Specify Conditions Using Query Operators A query filter document can


use the query operators to specify conditions in the following form:

{hf ield1i : {hoperator1i : hvalue1i} , · · · }

The following example retrieves all documents from the inventory collection
where status equals either ”A” or ”D”:

db.inventory.f ind ({status : {$in : [”A”, ”D”]}})

Page 11
Database Systems - Unit V Nithya BN

Specify AND Conditions A compound query can specify conditions for


more than one field in the collection’s documents. Implicitly, a logical AND
conjunction connects the clauses of a compound query so that the query selects
the documents in the collection that match all the conditions.
The following example retrieves all documents in the inventory collection where
the status equals ”A” and qty is less than ($lt) 30 :
db.inventory.f ind ({status : ”A”, qty : {$lt : 30}})

Specify OR Conditions Using the $or operator, you can specify a compound
query that joins each clause with a logical OR conjunction so that the query
selects the documents in the collection that match at least one condition.
The following example retrieves all documents in the collection where the status
equals ”A” or qty is less than ($lt) 30 :
db.inventory.f ind ({$or : [{status : ”A”} , {$lt : 30}]})

Specify AND as well as OR Conditions In the following example, the


compound query document selects all documents in the collection where the
status equals ”A” and either qty is less than ($lt) 30 or item starts with the
character p:

Query on Embedded/Nested Documents

Match an Embedded/Nested Document To specify an equality con-


dition on a field that is an embedded/nested document, use the query filter
document {hf ieldi : hvaluei}where hvaluei is the document to match.
For example, the following query selects all documents where the field size equals
the document {h : 14, w : 21, uom : ”cm”} :
db.inventory.f ind ({size : {h : 14, w : 21, uom : ”cm”}})

Query on Nested Field To specify a query condition on fields in an


embedded/nested document, use dot notation(”f ield.nestedF ield”) .

Specify Equality Match on a Nested Field The following example


selects all documents where the field uom nested in the size field equals ”in”:

db.inventory.f ind ({”size.uom” : ”in”})

Page 12
Database Systems - Unit V Nithya BN

Specify Match using Query Operator A query filter document can


use the query operators to specify conditions in the following form:
{hf ield1i : {hoperator1i : hvalue1i} , · · · }
The following query uses the less than operator($lt) on the field h embedded in
the size field:
db.inventory.f ind ({”size.h” : {$lt : 15}})

Specify AND Condition The following query selects all documents where
the nested field h is less than 15, the nested field uom equals ”in”, and the status
field equals ”D”:
db.inventory.f ind ({”size.h” : {$lt : 15} , ”size : uom” : ”in”, status : ”D”})

Query an Array

Match an Array To specify equality condition on an array, use the query


document {hf ieldi : hvaluei} where hvaluei is the exact array to match, includ-
ing the order of the elements.
The following example queries for all documents where the field tags value is an
array with exactly two elements, ”red” and ”blank”, in the specified order:
db.inventory.f ind ({tags : [”red, ”blank”]})
If, instead, you wish to find an array that contains both the elements ”red” and
”blank”, without regard to order or other elements in the array, use the $all
operator:
db.inventory.f ind ({tags : {$all : [”red”, ”blank”]}})

Query an Array for an Element To query if the array field contains


at least one element with the specified value, use the filter {hf ieldi : hvaluei}
where hvalueiis the element value.
The following example queries for all documents where tags is an array that
contains the string ”red” as one of its elements:

db.inventory.f ind ({tags : ”red”})

Specify Multiple Conditions for Array Elements When specifying


compound conditions on array elements, you can specify the query such that
either a single array element meets these condition or any combination of array
elements meets the conditions.

Query an Array with Compound Filter Conditions on the Array


Elements The following example queries for documents where the dim cm
array contains elements that in some combination satisfy the query conditions;
e.g., one element can satisfy the greater than 15 condition and another element
can satisfy the less than 20 condition, or a single element can satisfy both:
db.inventory.f ind ({dim cm : {$gt : 15, $lt : 20}})

Page 13
Database Systems - Unit V Nithya BN

Query for an Array Element that Meets Multiple Criteria Use


$elemMatch operator to specify multiple criteria on the elements of an array
such that at least one array element satisfies all the specified criteria.
The following example queries for documents where the dim cm array contains
at least one element that is both greater than ($gt) 22 and less than ($lt) 30:
db.inventory.f ind ({dim cm : {$eleM atch : {$gt : 22, $lt : 30}}})

Query for an Element by the Array Index Position Using dot no-
tation, you can specify query conditions for an element at a particular index or
position of the array. The array uses zero-based indexing.
The following example queries for all documents where the second element in
the array dim cm is greater than 25:
db.inventory.f ind ({”dim cm.1” : {$gt : 25}})

Query an Array by Array Length Use the $size operator to query for
arrays by number of elements. For example, the following selects documents
where the array tags has 3 elements.
db.inventory.f ind ({”tags” : {$size : 3}})

1.8.3 Update Operation


Update methods are as follows:
db.collection.updateOne (hf ilteri , hupdatei , hoptionsi)

db.collection.updateM any (hf ilteri , hupdatei , hoptionsi)

db.collection.replaceOne (hf ilteri , hupdatei , hoptionsi)

Update Documents in a Collection To update a document, MongoDB


provides update operators, such as $set, to modify field values.
To use the update operators, pass to the update methods an update document
of the form:

{
<update operator>: { <field1>: <value1>, ... },
<update operator>: { <field2>: <value2>, ... },
...
}

Some update operators, such as $set, will create the field if the field does not
exist.

Page 14
Database Systems - Unit V Nithya BN

Update a Single Document The following example uses the db.collection.updateOne()


method on the inventory collection to update the first document where item
equals ”paper”:

db.inventory.updateOne(
{ item: "paper" },
{
$set: { "size.uom": "cm", status: "P" },
$currentDate: { lastModified: true }
}
)
The update operation:

• uses the $set operator to update the value of the size.uom field to ”cm”
and the value of the status field to ”P”,
• uses the $currentDate operator to update the value of the lastModified
field to the current date. If lastModified field does not exist, $currentDate
will create the field.

db.inventory.updateMany(
{ "qty": { $lt: 50 } },
{
$set: { "size.uom": "in", status: "P" },
$currentDate: { lastModified: true }
}
)

db.inventory.replaceOne(
{ item: "paper" },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ]
)

1.8.4 Delete Operation


Delete methods are as follows:
db.collection.deleteMany()
db.collection.deleteOne()

Delete All Documents To delete all documents from a collection, pass an


empty filter document to the db.collection.deleteMany() method.
The following example deletes all documents from the inventory collection:

db.inventory.deleteMany()
The method returns a document with the status of the operation.

Page 15
Database Systems - Unit V Nithya BN

Delete All Documents that Match a Condition You can specify criteria,
or filters, that identify the documents to delete. The filters use the same syntax
as read operations.
To specify equality conditions, use hf ieldi : hvaluei expressions in the query
filter document:
{hf ieldi : hvaluei , · · · }
A query filter document can use the query operators to specify conditions in
the following form:
{hf ield1i : {hoperator1i : hvalue1i} , · · · }
To delete all documents that match a deletion criteria, pass a filter parameter
to the deleteMany() method.
The following example removes all documents from the inventory collection
where the status field equals ”A”:
db.inventory.deleteMany({ status : "A" })
The method returns a document with the status of the operation. For more
information and examples, see deleteMany().

Delete Only One Document that Matches a Condition To delete at


most a single document that matches a specified filter (even though multi-
ple documents may match the specified filter) use the db.collection.deleteOne()
method.
The following example deletes the first document where status is ”D”:

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

1.8.5 Bulk Write Operations


MongoDB provides clients the ability to perform write operations in bulk. Bulk
write operations affect a single collection. MongoDB allows applications to
determine the acceptable level of acknowledgement required for bulk write op-
erations.
The db.collection.bulkWrite() method provides the ability to perform bulk in-
sert, update, and remove operations. MongoDB also supports bulk insert through
the db.collection.insertMany().

Ordered vs Unordered Operations Bulk write operations can be either


ordered or unordered.
With an ordered list of operations, MongoDB executes the operations serially. If
an error occurs during the processing of one of the write operations, MongoDB
will return without processing any remaining write operations in the list. See
ordered Bulk Write.
With an unordered list of operations, MongoDB can execute the operations
in parallel, but this behavior is not guaranteed. If an error occurs during the
processing of one of the write operations, MongoDB will continue to process
remaining write operations in the list.
Executing an ordered list of operations on a sharded collection will generally be
slower than executing an unordered list since with an ordered list, each operation
must wait for the previous operation to finish.

Page 16
Database Systems - Unit V Nithya BN

By default, bulkWrite() performs ordered operations. To specify unordered


write operations, set ordered : false in the options document.

bulkWrite() Methods bulkWrite() supports the following write operations:


• insertOne

• updateOne
• updateMany
• replaceOne
• deleteOne

• deleteMany
Each write operation is passed to bulkWrite() as a document in an array.
For example, the following performs multiple write operations:
The characters collection contains the following documents:

{ "_id" : 1, "char" : "Brisbane", "class" : "monk", "lvl" : 4 },


{ "_id" : 2, "char" : "Eldon", "class" : "alchemist", "lvl" : 3 },
{ "_id" : 3, "char" : "Meldane", "class" : "ranger", "lvl" : 3 }
The following bulkWrite() performs multiple operations on the collection:

try {
db.characters.bulkWrite(
[
{ insertOne :
{
"document" :
{
"_id" : 4, "char" : "Dithras", "class" : "barbarian", "lvl" : 4
}
}
},
{ insertOne :
{
"document" :
{
"_id" : 5, "char" : "Taeln", "class" : "fighter", "lvl" : 3
}
}
},
{ updateOne :
{
"filter" : { "char" : "Eldon" },
"update" : { $set : { "status" : "Critical Injury" } }
}
},
{ deleteOne :

Page 17
Database Systems - Unit V Nithya BN

{ "filter" : { "char" : "Brisbane" } }


},
{ replaceOne :
{
"filter" : { "char" : "Meldane" },
"replacement" : { "char" : "Tanys", "class" : "oracle", "lvl" : 4 }
}
}
]
);
}
catch (e) {
print(e);
}

The operation returns the following:


{
"acknowledged" : true,
"deletedCount" : 1,
"insertedCount" : 2,
"matchedCount" : 2,
"upsertedCount" : 0,
"insertedIds" : {
"0" : 4,
"1" : 5
},
"upsertedIds" : {
}
}

1.9 Aggregation
Aggregation operations process data records and return computed results. Ag-
gregation operations group values from multiple documents together, and can
perform a variety of operations on the grouped data to return a single result.
MongoDB provides three ways to perform aggregation: the aggregation pipeline,
the map-reduce function, and single purpose aggregation methods.

Aggregation Pipeline MongoDB’s aggregation framework is modeled on the


concept of data processing pipelines. Documents enter a multi-stage pipeline
that transforms the documents into an aggregated result. For example:

db.orders.aggregate([
{ $match: { status: "A" } },
{ $group: { _id: "$cust_id", total: { $sum: "$amount" } } }
])

Page 18
Database Systems - Unit V Nithya BN

First Stage:
The $match stage filters the documents by the status field and passes to the
next stage those documents that have status equal to ”A”.
Second Stage:
The $group stage groups the documents by the cust id field to calculate the sum
of the amount for each unique cust id.

The most basic pipeline stages provide filters that operate like queries and doc-
ument transformations that modify the form of the output document.
Other pipeline operations provide tools for grouping and sorting documents by
specific field or fields as well as tools for aggregating the contents of arrays,
including arrays of documents. In addition, pipeline stages can use operators
for tasks such as calculating the average or concatenating a string.
The pipeline provides efficient data aggregation using native operations within
MongoDB, and is the preferred method for data aggregation in MongoDB.
The aggregation pipeline can operate on a sharded collection.
The aggregation pipeline can use indexes to improve its performance during
some of its stages. In addition, the aggregation pipeline has an internal opti-
mization phase.

Single Purpose Aggregation Operations MongoDB also provides


db.collection.estimatedDocumentCount(),
db.collection.count() and db.collection.distinct().
All of these operations aggregate documents from a single collection. While
these operations provide simple access to common aggregation processes, they
lack the flexibility and capabilities of an aggregation pipeline.

Page 19
Database Systems - Unit V Nithya BN

1.10 Map-Reduce
An aggregation pipeline provides better performance and usability than a map-
reduce operation.
Map-reduce operations can be rewritten using aggregation pipeline operators,
such as $group, $merge, and others.
For map-reduce operations that require custom functionality, MongoDB pro-
vides the $accumulator and $function aggregation operators starting in version
4.4.

1.10.1 Example
Create a sample collection orders with these documents:
db.orders.insertMany([
{ _id: 1, cust_id: "Ant O. Knee", ord_date:
new Date("2020-03-01"), price: 25,
items: [ { sku: "oranges", qty: 5, price: 2.5 },
{ sku: "apples", qty: 5, price: 2.5 } ], status: "A" },
{ _id: 2, cust_id: "Ant O. Knee", ord_date:
new Date("2020-03-08"), price: 70,

Page 20
Database Systems - Unit V Nithya BN

items: [ { sku: "oranges", qty: 8, price: 2.5 },


{ sku: "chocolates", qty: 5, price: 10 } ], status: "A" },
{ _id: 3, cust_id: "Busby Bee", ord_date:
new Date("2020-03-08"), price: 50,
items: [ { sku: "oranges", qty: 10, price: 2.5 },
{ sku: "pears", qty: 10, price: 2.5 } ], status: "A" },
{ _id: 4, cust_id: "Busby Bee", ord_date:
new Date("2020-03-18"), price: 25,
items: [ { sku: "oranges", qty: 10, price: 2.5 } ], status: "A" },
{ _id: 5, cust_id: "Busby Bee", ord_date:
new Date("2020-03-19"), price: 50,
items: [ { sku: "chocolates", qty: 5, price: 10 } ],
status: "A"},
{ _id: 6, cust_id: "Cam Elot", ord_date:
new Date("2020-03-19"), price: 35,
items: [ { sku: "carrots", qty: 10, price: 1.0 },
{ sku: "apples", qty: 10, price: 2.5 } ], status: "A" },
{ _id: 7, cust_id: "Cam Elot", ord_date:
new Date("2020-03-20"), price: 25,
items: [ { sku: "oranges", qty: 10, price: 2.5 } ],
status: "A" },
{ _id: 8, cust_id: "Don Quis", ord_date:
new Date("2020-03-20"), price: 75,
items: [ { sku: "chocolates", qty: 5, price: 10 },
{ sku: "apples", qty: 10, price: 2.5 } ], status: "A" },
{ _id: 9, cust_id: "Don Quis", ord_date:
new Date("2020-03-20"), price: 55,
items: [ { sku: "carrots", qty: 5, price: 1.0 },
{ sku: "apples", qty: 10, price: 2.5 },
{ sku: "oranges", qty: 10, price: 2.5 } ], status: "A" },
{ _id: 10, cust_id: "Don Quis", ord_date:
new Date("2020-03-23"), price: 25,
items: [ { sku: "oranges", qty: 10, price: 2.5 } ], status: "A" }
])

Return the Total Price Per Customer Perform the map-reduce operation
on the orders collection to group by the cust id, and calculate the sum of the
price for each cust id:
1. Define the map function to process each input document:
• In the function, this refers to the document that the map-reduce
operation is processing.
• The function maps the price to the cust id for each document and
emits the cust id and price.

var mapFunction1 = function() {


emit(this.cust_id, this.price);
};

Page 21
Database Systems - Unit V Nithya BN

2. Define the corresponding reduce function with two arguments keyCustId


and valuesPrices:

• The valuesPrices is an array whose elements are the price values


emitted by the map function and grouped by keyCustId.
• The function reduces the valuesPrice array to the sum of its elements.

var reduceFunction1 = function(keyCustId, valuesPrices) {


return Array.sum(valuesPrices);
};

3. Perform map-reduce on all documents in the orders collection using the


mapFunction1 map function and the reduceFunction1 reduce function:

db.orders.mapReduce(
mapFunction1,
reduceFunction1,
{ out: "map_reduce_example" }
)

This operation outputs the results to a collection named map reduce example.
If the map reduce example collection already exists, the operation will re-
place the contents with the results of this map-reduce operation.
4. Query the map reduce example collection to verify the results:

db.map_reduce_example.find().sort( { _id: 1 } )

The operation returns these documents:

{ "_id" : "Ant O. Knee", "value" : 95 }


{ "_id" : "Busby Bee", "value" : 125 }
{ "_id" : "Cam Elot", "value" : 60 }
{ "_id" : "Don Quis", "value" : 155 }

Page 22

You might also like