DB113 Module 14 NoSQL - MongoDB Query Language
DB113 Module 14 NoSQL - MongoDB Query Language
2
Installation Information
● Use MongoDB on local machine
○ Editions Use this for class work
■ Community edition: free use
● Download: [Link]
■ Enterprise edition: add LDAP and Kerberos support, on-disk encryption, auditing,
etc..
○ Start up the MongoDB
■ If you install MongoDB as a service -- Mongodb will startup automatically when you
power up your PC
■ If you do not install MongoDB as a service -- must start Mongo manually
■ To start the Mongodb shell: "C:\Program Files\MongoDB\Server\4.4\bin\[Link]" (or
change “C:\Program Files\MongoDB\” to your own installation path)
● Use cloud-based MongoDB Atlas:
○ Follow instructions on MongoDB website (Free use after registration)
3
Usage Information
● Use MongoDB on local machine
○ Use Compass
■ It comes together with MongoDB server installation package
■ Will start up automatically after installation.
■ Go to “Open MongoDB Shell” and you can start typing MQL queries.
■ This is the easiest way to start using location installation
○ Use MongoDB Shell This is my installation
path, (change “D:\Program
■ Download [Link] Files\MongoDB\Shell\” to
■ Installing and starting up the server, start the Mongodb shell by: your own installation path)
"D:\Program Files\MongoDB\Shell\[Link]"
■ When the shell ask you for a connection string, type: mongodb://localhost:27017/
● Use cloud-based MongoDB Atlas
○ Follow the instruction on the web page; should be straightforward
4
Using the Shell and Query Documentation
● In the shell, you can always get help by typing: help and [Link]()
○ And find out more from there
● To practice example MongoDB query language usages (example sessions)
○ [Link]
● For more detailed information: [Link]
5
MongoDB Data Model
● The MongoDB Data model consists of
○ Databases: analogous to RDB databases Unique and powerful feature of MongoDB
Database Database
Collection Table
Document Row
6
MongoDB Query Language
● MongoDB Query Language
○ The query language of MongoDB (a.k.a. called MQL)
● Three key features of MQL
○ Queries
■ Constructed using JSON-like documents.
■ The query documents specify the criteria for selecting data documents.
■ JSON-like documents are used both to store data and to express queries
○ Operators
■ Embedded operators in query documents can implement various kinds of queries
○ Pipelines
■ Expressed as an array in JSON document.
■ Enable the implementation of very powerful queries
7
One can create an empty collection
explicitly using “[Link]()”.
Mainly used to create a collection with
Creating Database and Collection specific options
db, collection ,document a = 3+5 , a a
8
Collections
● Collection Shema
○ In MongoDB, a collection can work with or without a schema
○ Without schema (default): within a collection:
■ The documents can have the different shapes and different fields
■ The same field name can have different data types across different documents.
○ With schema: enforce JSON schema validation for a collection
■ MongoDB supports draft 4 of JSON Schema, including core specification and validation
specification, with some differences. For details, see Extensions and Omissions.
■ For more information about JSON Schema, see the official website.
■ Use “[Link]()” with the $jsonSchema operator to enforce schema
● UUID
○ Each collection is assigned an immutable UUID.
■ Remains the same across all members of a replica set and shards of the collection
○ To retrieve the UUID of a collection: [Link]()
9
Example of [Link]("students", {
validator: {
Collection with $jsonSchema: {
bsonType: "object",
Schema Validation title: "Student Object Validation",
required: [ "address", "major", "name", "year" ],
properties: {
the following command to create name: {
a students collection and use bsonType: "string",
description: "'name' must be a string and is required"
the $jsonSchema operator to },
set schema validation rules: year: {
bsonType: "int",
minimum: 2017,
maximum: 3017,
description: "'year' must be an integer in [ 2017, 3017 ] and is required"
},
gpa: {
bsonType: [ "double" ],
description: "'gpa' must be a double if the field exists"
}
}
}
}
})
10
Views
● Read-only, and can be defined on
○ Aggregation pipeline → powerful
○ Collections, other views.
● Purposes
○ To exclude private or confidential data from a collection.
○ To add computed fields to a collection
○ To join data from two different related collections.
● On-demand Materialized views: a powerful mechanism in MongoDB
○ Are really “incremental aggregation pipelines”
● Views can be created by
[Link](
[Link]( "<viewName>",
"<viewName>", {
"<source>", "viewOn" : "<source>",
[<pipeline>], "pipeline" : [<pipeline>],
{ "collation" : { <collation> } "collation" : { <collation> }
}) })
11
Documents
● Expressed in JSON format, stored in BSON format
● Serve several important purposes
○ Store data
○ Express all sorts of conditions in MongoDB query operations
■ Query filter condition, update condition, delete condition, index condition, etc.
○ Convey the execution results of an MQL operations
● Each document stored in a collection requires a unique _id field
○ Acts as a primary key.
● When inserting a document into collection, the user can
○ Specify _id explicitly
■ The _id value can be of any BSON data type, except for array.
○ Insert without specifying _id
■ MongoDB automatically generates an ObjectId for the _id field.
12
MongoDB Basic
CRUD Operations
13
High-Level Commands - DB Note: mongoDB commands are
case sensitive
14
High-Level Commands - Collection
MongoDB SQL Action
select project
[Link]({} ,{name :1})
15
CRUD - Create (Insert) Document
● Create (Insert)
The whole command is actually
○ [Link](<doc>) a JavaScript function call
○ [Link]([<doc1>,<doc2>,...])
16
CRUD - Read (Find) Document Conditions may contain operators.
Operators are of the form $xxx
● Read (Find)
○ [Link]( <query>, <projection>, <options> )
○ [Link]( <query>, <projection>, <options> )
0 1 show
1: project this field; 0: don’t project this field
17
CRUN - A Closer Look at MongoDB Commands
● The parameters in MongoDB function call are often optional
● Example: using find() as example
○ To find user documents in the “users” collection:
The function [Link]( <query>, <projection>, <options> )
Can be called as
[Link]() → all user docs
[Link](<query>) → user docs matching query condition
[Link](<query>, <projection>) → matching condition and project
[Link](<query>, <projection>, <options>) → with additional options
18
CRUD - Update Document
● UPDATE (update, replace)
○ [Link](<filter>, <update>, <options>)
○ [Link]( <filter>, <update>, <options> )
○ [Link]( <filter>, <replacement doc>, <options> )
● One vs Many
○ UpdateOne(): Update the first document satisfying the filter condition
○ UpdateMany(): update all documents satisfying the filter condition
● Any field “updated” but previously did not exists will be created Field update operators:
● Why both update and replace? $currentDate
○ Update → it’s still the same document (same _ID) $inc
○ Replace → a different document (different _ID) $min
$max
$mul
$rename
$set
$setOnInsert
18 status "reject"
status $unset 19
_id age
CRUD - Delete Document
● DELETE (delete)
○ [Link]. deleteOne(<filter>, <options>)
○ [Link]. deleteMany(<filter>, <options>)
20
Deeper Look at MQL and MQL Shell ✋wait a minute
● MongoDB is designed based on JavaScript
● MongoDB Query Language is actually compatible with JavaScript code
○ All MQL commands are valid JS statements
● MongoDB Shell can actually accept most JavaScript language
○ For example, you can write something like the following in the shell prompt:
let ageToFind = 30;
let cityToMatch = "Paris";
let query = { age: ageToFind, city: cityToMatch };
[Link].find(query);
○ Or even the following
function greetUser(user) { Very useful if you are a
print("Hello, " + [Link] + "!"); JavaScript programmer!
}
[Link].findOne({ city: "London" }).forEach(greetUser);
21
More on Query Filter Documents
Consider the following example
{
“_id” : ObjectId(“5e009………”), System generates ID automatically
“Item” : “postcard”,
“qty” : “A”,
“status” : “A”,
“size” : {
“h” : 10, Nested document
“w” : “15.25,
“uom” : “cm”
}
“tags” : [
“blue”
]
}
22
When the field is a simple word, you can do without quote signs.
Need quote signs When the field is of the form “x.y”, you need the quote sign
[Link]( { "[Link]": "cm" } ) Requires a join Retrieve data from nested document → use
dot expression.
[Link]( { size: { h: 14, w: 21, uom: AND in where clause Multiple condition from nested document →
"cm" } } ) comma in filter doc.
[Link]( {}, { item: 1, status: 1 } ); “Select item, status from inventory” Select with projection
[Link]( {}, { _id: 0, item: 1, status: 2nd mongoDB query excludes system
1 } ); generated ID.
[Link]( { status: { $in: [ "A", "D" ] } “SELECT * FROM inventory WHERE $in operator
}) status in ("A", "D")”
[Link]( { status: "A", qty: { $lt: 30 } “SELECT * FROM inventory WHERE $lt operator
}) status = "A" AND qty < 30”
[Link]( { $or: [ { status: "A" }, { SELECT * FROM inventory WHERE ‘$or’ operator. AND is express with comma,
qty: { $lt: 30 } } ] } ) status = "A" OR qty < 30 OR requires explicit $or operator
23
Query Filter Document on Arrays
Note that in this example both the color tags and the dimension of the
inventory items are expressed as arrays
24
SQL array
[Link]( { tags: ["red", "blank"] } ) Exact: find document with a “tags” array ["red", "blank"]
[Link]( { tags: { $all: ["red", "blank"] } } ) Contain: find document with a “tags” array which contains
“red” and “bank” (can have other tags)
[Link]( { tags: "red" } ) Contain: when there is only element in the condition
[Link]( { dim_cm: { $gt: 25 } } ) dim_cm: [ 14, 21 ] Find document whose “dim_cm” contains one element > 25
[Link]( { dim_cm: { $gt: 20, $lt: 15 } } ) Both conditions are met. Not necessarily by the same
[Link]( { dim_cm: { $gt: 15, $lt: 20 } } ) element
[Link]( { dim_cm: { $elemMatch: { $gt: 22, $lt: Same element matches both conditions
30 } } } )
[Link]( { "dim_cm.1": { $gt: 25 } } ) Condition for array index position. “dim_cm.1” means the 2nd
position of “dim_cm” array.
25
Query Filter Documents for Null and Non-existence
MongoDB SQL Action
[Link]( { item: null } ) No exact match in SQL Find document which either does not contain “item”
or whose “item” is null.
[Link]( { item : { $type: 10 } Select * from inventory Return only the document whose “item” is null.
}) where type is null; Type 10 is BSON type # for null
[Link]( { item : { $exists: No match in SQL Return only the document which does not contain
false } } ) an ‘item’ field.
26
More on Updates
MongoDB SQL Action
[Link]( Replace the old document with the new document (with
{ item: "paper" }, different _id)
{ item: "paper", instock: [ { warehouse: "A",
qty: 60 }, { warehouse: "B", qty: 40 } ] }
)
27
go DB feature
Important Mon
Text Search on Documents
MongoDB Action
[Link]( { name: "text", description: "text" } ) Create text index based both “name” and
“description” fields
[Link]( { $text: { $search: "java coffee shop" } } ) Find shops with words “java” or “coffee” or “shop”
[Link]( { $text: { $search: "\"coffee shop\"" } } ) Find shops containing the phase “coffee shop”.
[Link]( { $text: { $search: "java coffee -shop" } }, { score: { Find all documents containing “java coffee”, but
$meta: "textScore" } } ).sort( { score: { $meta: "textScore" } } ) not “shop”, sort the result by their matching score
28
MongoDB Aggregation
Pipelines
29
Aggregation in MongoDB
● Aggregation
○ Group values from multiple documents,
○ Perform a variety of operations on the grouped data,
○ To return a single result.
● MongoDB provide three ways to perform aggregation:
○ Single Purpose Aggregation Operations
■ Similar to SQL aggregations
■ Less flexible / powerful, but easy for quick uses
○ Map-Reduce:
■ Follow the map-reduce paradigm, can/should be replaced by map-reduce aggregation
pipelines
○ Aggregation Pipeline
■ ⇒ A flexible and powerful way to do aggregation p o rt a n t M o n goDB feature
Im
30
Single Purpose Aggregate Operations
● Single purpose aggregation operations
○ [Link](<options>)
○ [Link](<filter>, <options>)
○ [Link](<field>, <filter>, <options>)
[Link]( {} ) Select count(*) from orders Similar to find() but only count
the # of matching documents
32
3 "hello"
Aggregation Expression
$add $multiply
33
Syntax of Pipeline Stages and Expressions
● [Link]([
{ $match: { status: "A" } },
{ $group: { _id: "$cust_id", total: { $sum: "$amount" } } }
]) Aggregation expression
Pipeline of two stages
● [Link]([
{ $project: {item: 1, total: {$add: ["$price", "$fee"]}}}
])
Aggregation expression
34
Aggregation Pipeline Example
● [Link]([
{ $match: { status: "A" } },
{ $group: { _id: "$cust_id", total: { $sum: "$amount" } } }
])
output
● 1st Stage: $match
○ Filters out the documents with the status= "A" and passes to the next stage
● 2nd Stage: $group
○ Groups the documents by the cust_id, and calculate the sum of the amount for each cust_id
36
Basic and Interesting Stages (Nonexhaustive)
● Reshaping documents
○ $addFields, $project, $replaceWith
○ $set: add new fields, $unset: delete fields
○ $unwind: break an array, for each element in an array, create a new document
● Basic Aggregate
○ $group, $counts
● Statistics $bucket 0-10 10-20
○ $bucket, $bucketAuto $bucketAuto
know these!
Make sure you
$collStats
○ $collStats
$sample
○ $sample
● Select/Join/Output
○ $match: select, $seach: full text search
○ $lookup: left outer join
○ $unionWith: union of documents from with another collection
○ $merge: incorporate various outputs into a collection
○ $out: output into a collection
○ $sort: sort the output
37
Aggregation Expression Examples
Expression operators
[Link]( [ [Link]([
{ _id: 1, startTemp: 50, endTemp: 80 }, {
{ _id: 2, startTemp: 40, endTemp: 40 }, $project: { delta: { $abs: { $subtract: [ "$startTemp", "$endTemp" ] } } }
{ _id: 3, startTemp: 90, endTemp: 70 }, }
{ _id: 4, startTemp: 60, endTemp: 70 } ])
])
38
use the $accumulator and $function
aggregation operators to define custom
More on Aggregation Expressions aggregation expressions in JavaScript.
● Aggregation expression
○ Of document structure
○ Can be nested
○ Can include field paths, literals, system variables, and expression operators.
● Field Paths
○ For accessing fields in the input documents.
○ To specify a field path, prefix the field name or the dotted field name with a dollar sign “$”
■ E.g. "$user" specifies the field path for the user field
■ E.g. "$[Link]" specifies the field path to "[Link]" field.
● Literals
○ Literals can be of any type, representing the written value
○ Note: MongoDB parses string literals that start with a dollar sign $ as a path to a field and
numeric/boolean literals in expression objects as projection flags. To avoid parsing literals, use the
$literal expression.
○ $literal format: { $literal: <value> }. E.g.: { $literal: { $add: [ 2, 3 ] } } is evaluated to { "$add" : [ 2, 3 ] }
39
Aggregation Variables (System Variables)
Variable Brief Description
● Aggregation variables
$$NOW Returns the current datetime value, which remains constant across all members
○ MongoDB provides
of the deployment and throughout the aggregation pipeline. (Available in 4.2+)
various aggregation
system variables for $$CLUSTER_ Returns the current timestamp value, which is same across all members of the
TIME deployment and remains constant throughout the aggregation pipeline. For
use in expressions. To replica sets and sharded clusters only. (Available in 4.2+)
access variables,
$$ROOT References the root document, i.e. the top-level document.
prefix the variable
name with $$. $$CURRENT References the start of the field path, defaults to ROOT but can be changed.
40
MongoDB uses the terms aggregation expression
operators, aggregation operators, and expression
Expression Operators operators interchangeably.
41
Categories of Expression Operators
Category Example Category Example
Comparison $eq, $gt, $gte, $lt, $lte, $ne String $concat, $split, $substr
Data Size $binarySize, $bsonSize Variable $let: set variable in the scope of subexpression
Date $hour, $dayOfMonth, $dayOfWeek Window return values from a defined span of documents
Name Description
$addToSet Returns an array of unique expression values for each group. Order of the array elements is undefined.
$mergeObjects Returns a document created by combining the input documents for each group.
$first, $firstN Returns a value from the first (n) document for each group. Order is only defined if the documents are in a defined
order.
$last, $lastN Returns a value from the last (n) document for each group. Order is only defined if the documents are in a defined
order.
$bottom, $bottomN, ……
$top, topN
44
Example of Vertical Aggregation ($Group-like)
Input collection:
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-01T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-02-03T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-03T09:05:00Z") }
{ "_id" : 4, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-02-15T08:00:00Z") }
{ "_id" : 5, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-15T09:12:00Z") }
[Link]( Output:
[ { "_id" : "xyz", "avgAmount" : 37.5, "avgQuantity" : 7.5 }
{ { "_id" : "jkl", "avgAmount" : 20, "avgQuantity" : 1 }
$group: { "_id" : "abc", "avgAmount" : 60, "avgQuantity" : 6 }
{ group by id CREATE TABLE sales ( INSERT INTO sales (id, item, price,
_id: "$item", id INT, quantity, date) VALUES
item VARCHAR(10), (1, 'abc', 10, 2, '2014-01-01 08:00:00'),
avgAmount: { $avg: { $multiply: [ "$price", "$quantity" ] } }, price DECIMAL(10, 2), (2, 'jkl', 20, 1, '2014-02-03 09:00:00'),
avgQuantity: { $avg: "$quantity" } quantity INT, (3, 'xyz', 5, 5, '2014-02-03 09:05:00'),
date DATETIME (4, 'abc', 10, 10, '2014-02-15 09:00:00'),
} ); (5, 'xyz', 5, 10, '2014-02-15 09:12:00');
}
SELECT
] item,
AVG(price * quantity) AS avgAmount,
) AVG(quantity) AS avgQuantity
FROM
sales
GROUP BY 45
item;
Accumulators Used in Other Stages
● Used in the $project, $addFields, and $set stages
○ Available for use in these stages but not as accumulators. When used in these stages, these
operators do not maintain their state and can take as input either a single argument or multiple
arguments. For details, refer to the specific operator page.
Name Description
$stdDevPop, Returns the population (sample) standard deviation of the input values.
$stdDevSamp
… …
46
Example of Horizontal Aggregation ($Project-like)
Input collection:
{ "_id": 1, "quizzes": [ 10, 6, 7 ], "labs": [ 5, 8 ], "final": 80, "midterm": 75 }
{ "_id": 2, "quizzes": [ 9, 10 ], "labs": [ 8, 8 ], "final": 95, "midterm": 80 }
{ "_id": 3, "quizzes": [ 4, 5, 5 ], "labs": [ 6, 5 ], "final": 78, "midterm": 70 }
[Link]([
{ $project: { quizAvg: { $avg: "$quizzes"}, labAvg: { $avg: "$labs" },
examAvg: { $avg: [ "$final", "$midterm" ] } } }
])
Output:
{ "_id" : 1, "quizAvg" : 7.666666666666667, "labAvg" : 6.5, "examAvg" : 77.5 }
{ "_id" : 2, "quizAvg" : 9.5, "labAvg" : 8, "examAvg" : 87.5 }
{ "_id" : 3, "quizAvg" : 4.666666666666667, "labAvg" : 5.5, "examAvg" : 74 }
47
Aggregate Pipeline Example (1)
Example; membership data of a sports club
Query: List usernames ordered by joined month
{ [Link]( {
_id : "jane", [ "month_joined" : 1,
"name" : "ruth"
joined : ISODate("2011-03-02"), { $project : },
likes : ["golf", "racquetball"] {month_joined: { $month : "$joined" , {
} name : "$_id", _id : 0 } "month_joined" : 1,
{ }, "name" : "harold"
},
_id : "joe", { $sort : { month_joined : 1 } } {
joined : ISODate("2012-07-02"), ] "month_joined" : 1,
likes : ["tennis", "golf", "swimming"] ) "name" : "kate"
} }
{
…... "month_joined" : 2,
"name" : "jill"
}
{ [Link]( {
_id : "jane", [ "_id" : { "month_joined" : 1 },
joined : ISODate("2011-03-02"), { $project : { month_joined : { $month : "number" : 3
likes : ["golf", "racquetball"] "$joined" } } } , },
} { $group : { _id : {
{ {month_joined:"$month_joined"} , "_id" : { "month_joined" : 2 },
_id : "joe", number : { $sum : 1 } } }, "number" : 9
joined : ISODate("2012-07-02"), { $sort : { "_id.month_joined" : 1 } } },
likes : ["tennis", "golf", "swimming"] ] {
} ) "_id" : { "month_joined" : 3 },
…... "number" : 5
Note: Here each _id is a document with }
one fields: month_joined
{ [Link]( {
_id : "jane", [ "_id" : "golf", "number" : 33
},
joined : ISODate("2011-03-02"), { $unwind : "$likes" }, {
likes : ["golf", "racquetball"] { $group : { _id : "$likes" , number : { "_id" : "racquetball", "number" : 31
} $sum : 1 } } }, },
{ { $sort : { number : -1 } }, {
"_id" : "swimming", "number" : 24
_id : "joe", { $limit : 5 } },
joined : ISODate("2012-07-02"), ] {
likes : ["tennis", "golf", "swimming"] ) "_id" : "handball", "number" : 19
} },
{
…... "_id" : "tennis", "number" : 18
}
51
Basic Map-Reduce:
Example
● Starting in MongoDB 5.0, Map and reduce steps may
map-reduce is deprecated. involve many nodes. System
● Map-Reduce function should be takes care of distributed
implemented using aggregation processing for you
pipeline - for better performance
and usability.
● Useful aggregation pipeline
stages: $group, $merge, and
others.
52
Map-Reduce Example 1 (mapReduce Method)
Example: a sales database
Query: Return the total revenue for each customer
[Link]([
{ _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" }, var mapFunction1 = function() {
{ _id: 2, cust_id: "Ant O. Knee", ord_date: new Date("2020-03-08"), price: 70, items: [ { sku: "oranges",
qty: 8, price: 2.5 }, { sku: "chocolates", qty: 5, price: 10 } ], status: "A" }, emit(this.cust_id, [Link]);
{ _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" }, var reduceFunction1 = function(keyCustId,
{ _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" }, valuesPrices) {
{ _id: 5, cust_id: "Busby Bee", ord_date: new Date("2020-03-19"), price: 50, items: [ { sku: "chocolates", return [Link](valuesPrices);
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" }, [Link](
{ _id: 7, cust_id: "Cam Elot", ord_date: new Date("2020-03-20"), price: 25, items: [ { sku: "oranges", qty: mapFunction1,
10, price: 2.5 } ], status: "A" }, reduceFunction1,
{ _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" }, { out: "map_reduce_example" }
{ _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" }, db.map_reduce_example.find().sort( { _id:
{ _id: 10, cust_id: "Don Quis", ord_date: new Date("2020-03-23"), price: 25, items: [ { sku: "oranges", qty: 1})
10, price: 2.5 } ], status: "A" }
])
Map-reduce command
Data in MongoDB implementation 53
Map-Reduce Example 1-1 (Pipeline)
Return the total revenue for each customer $out: takes the documents returned by the aggregation
pipeline and writes them to a specified collection.
db.agg_alternative_1.find().sort( { _id: 1 } )
Data in
MongoDB Aggregate pipeline Query output
$match: filters the documents to pass only the $merge: merge the results of the aggregation pipeline to a specified
documents that match the specified condition(s) to collection. The $merge operator must be the last stage in the pipeline. If
the next pipeline stage. the specified collection does not exist, create the collection. 56
Incremental Map-Reduce
● If the map-reduce data set is constantly growing
○ ⇒ you may want to perform an incremental map-reduce rather than performing the
map-reduce operation over the entire data set each time.
● To perform incremental map-reduce:
○ 1. Run a map-reduce job over the current collection and output the result to a separate
collection.
○ 2. When you have more data to process, run subsequent map-reduce jobs with:
■ the query parameter that specifies conditions that match only the new documents.
■ the out parameter that specifies the reduce action to merge the new results into the
existing output collection.
57
var mapFunction = function() { var finalizeFunction = function(key,
var key = [Link]; reducedValue) {
Incremental var value = { total_time: [Link], count: 1, if ([Link] > 0)
reducedValue.avg_time =
avg_time: 0 };
MapReduce emit( key, value );
reducedValue.total_time / [Link];
Example };
};
return reducedValue;
61
Example: Real Find, Insert and Delete
[Link]( [Link]( [Link](
{ { {
find: <collection>, insert: <collection>, delete: <collection>,
filter: <document>, documents: [ <document>, <document>, deletes: [
sort: <document>, <document>, ... ], {
projection: <document>, ordered: <boolean>, q : <query>,
hint: <document or string>, maxTimeMS: <integer>, limit : <integer>,
skip: <int>, writeConcern: { <write concern> }, collation: <document>,
limit: <int>, bypassDocumentValidation: <boolean>, hint: <document|string>
batchSize: <int>, comment: <any> },
singleBatch: <bool>, } ...
comment: <any>, ) ],
maxTimeMS: <int>, comment: <any>,
readConcern: <document>, let: <document>, // Added in MongoDB 5.0
max: <document>, ordered: <boolean>,
min: <document>, writeConcern: { <write concern> },
returnKey: <bool>, maxTimeMS: <integer>
showRecordId: <bool>, }
tailable: <bool>, )
oplogReplay: <bool>,
noCursorTimeout: <bool>,
awaitData: <bool>,
…….
}
) 62