Skip to main content
MongoDB
By wiTTyMindsTech
MongoDB Overview
MongoDB is a cross-platform document-oriented database program. Classified as a NoSQL
database program, MongoDB uses JSON-like documents with schema. MongoDB is
developed by MongoDB Inc. and licensed under the Server Side Public License.
EMPLOYEE
NAME ID COMPANY
NoSQL DB Overview
Unlike MySQL and Oracle Databases.
NoSQL Deals in files .
NoSQL, which stand for "not only SQL," is an alternative to traditional relational databases in which
data is placed in tables and data schema is carefully designed before the database is built. NoSQL
databases are especially useful for working with large sets of distributed data.
JSON Introduction
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to
read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript
Programming Language Standard ECMA-262 3rd Edition - December 1999.
.
JSON Introduction
.
JSON Data types
At the granular level, JSON consist of 6 data types. First four data types (string, number, boolean and null)
can be referred as simple data types. Other two data types (object and array) can be referred as complex
data types.
1. string
2. number
3. boolean
4. null/empty
5. object
6. array
7.
MongoDB Overview
MongoDB Overview
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.
MongoDB Collections/Views
MongoDB stores documents in collections. Collections are analogous to tables in relational databases.
In addition to collections, MongoDB supports:
● Read-only Views (Starting in MongoDB 3.4)
● On-Demand Materialized Views (Starting in MongoDB 4.2
MongoDB Overview
MongoDB is free and open source database.
It is a NoSQL database use Json like schema.
It is a cross platform database and very easy to deploy on the cloud and server.
It is of the most important databases you can work with these days.
MongoDB makes working with data simple.
It prioritizes performance and efficiency.
It is very popular. MongoDB developers are in high demand.
MongoDB CRUD
● How to perform CRUD (Create, Read, Update, Delete) operations on
MongoDB databases
MongoDB Create
● db.collection.insertOne() New in version 3.2
● db.collection.insertMany() New in version 3.2
MongoDB Read
db.collection.find()
You can specify query filters or criteria that identify the documents to return.
MongoDB Update
● db.collection.updateOne() New in version 3.2
● db.collection.updateMany() New in version 3.2
● db.collection.replaceOne() New in version 3.2
MongoDB Delete
Delete operations remove documents from a collection. MongoDB provides the following methods to delete
documents of a collection:
● db.collection.deleteOne() New in version 3.2
● db.collection.deleteMany() New in version 3.2
MongoDB Objectives
● How to perform CRUD (Create, Read, Update, Delete) operations on MongoDB databases
● How to filter for data efficiently
● How to work with both the Mongo Shell and drivers (e.g. Node.js driver)
● How to increase performance by using indexes (and how to use the right indexes!)
● How to use the amazing “Aggregation Framework” that’s built into MongoDB
● What replica sets and sharding are
● How to use MongoDB Atlas — the cloud solution offered by MongoDB
● How to use the serverless platform (Stitch) offered by MongoDB
MongoDB Advantages
● Schema less − MongoDB is a document database in which one collection holds different documents. Number of fields,
content and size of the document can differ from one document to another.
● Structure of a single object is clear.
● No complex joins.
● Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query language that's nearly
as powerful as SQL.
● Tuning.
● Ease of scale-out − MongoDB is easy to scale.
● Conversion/mapping of application objects to database objects not needed.
● Uses internal memory for storing the (windowed) working set, enabling faster access of data.
● Scalability
●
MongoDB Objectives
● Download and install MongoDB
● Modify environment variables
● Start and stop mongoDB server
● Connect to MongoDB using python
● Perform CRUD operations
MongoDB Objectives
● Create a database
● Create a collection
● Insert documents
● Combine collections
● Import data into mongoDB
● Backup and restore mongoDB
● Create indexes
Why Use MongoDB?
● Document Oriented Storage − Data is stored in the form of JSON style documents.
● Index on any attribute
● Replication and high availability
● Auto-sharding
● Rich queries
● Fast in-place updates
● Professional support by MongoDB
Where to use MongoDB?
● Big Data
● Content Management and Delivery
● Mobile and Social Infrastructure
● User Data Management
● Data Hub
Where to use MongoDB?
● Big Data
● Content Management and Delivery
● Mobile and Social Infrastructure
● User Data Management
● Data Hub
MongoDB Data Modelling
Suppose a client needs a database design for his blog/website and see the differences between RDBMS and MongoDB
schema design. Website has the following requirements.
● Every post has the unique title, description and url.
● Every post can have one or more tags.
● Every post has the name of its publisher and total number of likes.
● Every post has comments given by users along with their name, message, data-time and likes.
● On each post, there can be zero or more comments
MongoDB Data Modelling
MongoDB Structure
{
_id: POST_ID
title: TITLE_OF_POST,
description: POST_DESCRIPTION,
by: POST_BY,
url: URL_OF_POST,
tags: [TAG1, TAG2, TAG3],
likes: TOTAL_LIKES,
comments: [
{
user:'COMMENT_BY',
message: TEXT,
dateCreated: DATE_TIME,
like: LIKES
},
{
user:'COMMENT_BY',
message: TEXT,
dateCreated: DATE_TIME,
like: LIKES
}
]
}
MongoDB Installation
$sudo apt-get update
$sudo apt install -y mongodb
$sudo systemctl status mongodb
$mongo --eval 'db.runCommand({ connectionStatus: 1 })'
$mongo
>show databases
>use mydb
>show collections
MongoDB Conf file and Settings
/etc/mongoDB.conf
A given mongo database is broken up into a series of BSON files on disk, with increasing size up to 2GB. BSON is its
own format, built specifically for MongoDB.
MongoDB login
$ mongo
Will try to connect to localhost
Using ip address and port
$ mongo --host 192.168.64.24:27017
Or
$mongo --host 192.168.64.24 --port 27017
MongoDB User Creation
1. Set up your user
use cool_db
db.createUser({
user: 'john',
pwd: 'secretPassword',
roles: [{ role: 'readWrite', db:'test'}]
})
MongoDB User Creation
mongo -u john -p secretPassword 192.168.64.24/test
MongoDB shell version v3.6.3
connecting to: mongodb://192.168.64.24:27017/test
MongoDB server version: 3.6.3
> db.inv.insert({"name":"abc1", "add":"street3"})
WriteResult({ "nInserted" : 1 })
MongoDB User Creation
1. Set up your user for read only
use cool_db
db.createUser({
user: 'Sya',
pwd: 'secretPassword',
roles: [{ role: 'read', db:'test'}]
})
MongoDB User Creation
$ mongo -u Syam -p secretPassword 192.168.64.24/test
MongoDB shell version v3.6.3
connecting to: mongodb://192.168.64.24:27017/test
MongoDB server version: 3.6.3
> db.inv.insert({"name":"abc1", "add":"street3"})
WriteResult({
"writeError" : {
"code" : 13,
"errmsg" : "not authorized on test to execute command { insert: "inv", ordered:
true, $db: "test" }"
}
})
MongoDB User login
$ mongo -u john -p secretPassword 192.168.64.24/test
MongoDB security authorization
security:
authorization: 'enabled'
MongoDB security authorization
Create a user before uncommenting auth=true in mongodb.conf
db.createUser(
... {
... user: "myUserAdmin",
... pwd: "abc123",
... roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
... }
... )
Again uncomment the auth=true and restart mongodb
mongo --host 192.168.64.24 --port 27017 -u myUserAdmin -p --authenticationDatabase admin
MongoDB Create DB
MongoDB use DATABASE_NAME is used to create database. The command will create a new database if it
doesn't exist, otherwise it will return the existing database.
If you didn't create any database, then collections will be stored in test database.
>use mydb
switched to db mydb
>db.dropDatabase()
MongoDB Create
Create collection explicitly
● db.createCollection("mycol", { capped : true, autoIndexId : true, size :
● 6142800, max : 10000 } )
MongoDB Data type
Int
Boolean
Float
String
etc
MongoDB Create Collection
The cool thing about MongoDB is that you need not to create collection before you insert document in it.
With a single command you can insert a document in the collection and the MongoDB creates that
collection on the fly.
Syntax: db.collection_name.insert({key:value, key:value…})
MongoDB Create Collection
db.beginnersbook.insert({
name: "Chaitanya",
age: 30,
website: "beginnersbook.com"
})
db.collection_name.insert
Find the Details
MongoDB Insert
db.post.insert([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
title: 'NoSQL Database',
description: "NoSQL database doesn't have tables",
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 20,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2013,11,10,2,35),
like: 0
}
]
}
])
MongoDB Collection insert methods
db.collection.insertOne()
Inserts a single document into a collection.
db.collection.insertMany()
db.collection.insertMany() inserts multiple documents into a collection
db.collection.insert()
db.collection.insert() inserts a single document or multiple documents into a collection
MongoDB Collection insert
The insert() Method
db.COLLECTION_NAME.insert(document)
Example:
db.mycol.insert({
_id: ObjectId(7df78ad8902c),
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
MongoDB Collection insert multiple
We can pass array of document also
:
db.post.insert([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
title: 'NoSQL Database',
description: "NoSQL database doesn't have tables",
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 20,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2013,11,10,2,35),
like: 0
}
]
MongoDB Collection find method
db.COLLECTION_NAME.find()
find() method will display all the documents in a non-structured way.
The pretty() Method
To display the results in a formatted way, you can use pretty() method.
Syntax
>db.mycol.find().pretty()
MongoDB Collection InsertOne
db.products.insertOne( { item: "card", qty: 15 } );
Products will be created automatically.
MongoDB Collection find method
>db.mycol.find().pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview" ,
"description": "MongoDB is no sql database" ,
"by": "tutorials point" ,
"url": "http://www.tutorialspoint.com" ,
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
MongoDB Collection insert data
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" }
]);
MongoDB Collection select data
db.inventory.find( {} )
This operation corresponds to the following SQL statement:
SELECT * FROM inventory
db.inventory.find( { status : "D" } )
This operation corresponds to the following SQL statement:
SELECT * FROM inventory WHERE status = "D"
MongoDB Collection select data
The following example retrieves all documents from the inventory collection where status equals either "A"or
"D":
db.inventory.find( { status: { $in: [ "A", "D" ] } } )
Select * from inventory where status in ( “A”,”D”)
MongoDB Collection select data
The following example retrieves all documents from the inventory collection where status equals either "A"or
"D":
db.inventory.find( { status: { $nin: [ "A", "D" ] } } )
Select * from inventory where status not in ( “A”,”D”)
MongoDB Collection select data
#count the number of resultant rows
db.inventory.count( { status: { $nin: [ "A", "D" ] } } )
db.inventory.find( { status: "A", qty: { $lt: 30 } } )
The operation corresponds to the following SQL statement:
SELECT * FROM inventory WHERE status = "A" AND qty < 30
OR in MongoDB
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.
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
SELECT * FROM inventory WHERE status = "A" OR qty < 30
db.inventory.find( { $and: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
Exporting mongodb collection in json file
$ sudo mongoexport --host 192.168.64.24 --db config --collection inventory --out
/home/parallels/status.json
OR in MongoDB
To query documents based on the OR condition, you need to use $or keyword. Following is the basic syntax of OR −
>db.mycol.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
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.
OR in MongoDB
Following example will show all the tutorials written by 'tutorials point' or whose title is 'MongoDB Overview'.
>db.mycol.find({$or:[{"by":"tutorials point"},{"title": "MongoDB Overview"}]}).pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "tutorials point",
"url": "http://www.tutorialspoint.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
AND and OR in MongoDB
where likes>10 AND (by = 'tutorials point' OR title = 'MongoDB Overview')'
>db.mycol.find({"likes": {$gt:10}, $or: [{"by": "tutorials point"},
{"title": "MongoDB Overview"}]}).pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "tutorials point",
"url": "http://www.tutorialspoint.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
insertMany
db.collection.insertMany(
[ <document 1> , <document 2>, ... ],
{
writeConcern: <document>,
ordered: <boolean>
}
)
insertMany check null
db.inventory.insertMany([
{ _id: 1, item: null },
{ _id: 2 }
])
db.inventory.find( { item: null } )
The { item : { $type: 10 } } query matches only documents that contain the item field whose value
isnull; i.e. the value of the item field is of BSON Type Null (type number 10) :
db.inventory.find( { item : { $type: 10 } } )
insertMany check null
db.inventory.find( { item : { $exists: false } } )
MongoDB Update
● db.collection.updateOne(<filter>, <update>, <options>)
● db.collection.updateMany(<filter>, <update>, <options>)
● db.collection.replaceOne(<filter>, <update>, <options>)
db.inventory.insertMany( [
{ item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" },
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" },
{ item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
{ 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" },
{ item: "sketchbook", qty: 80, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.5, uom: "cm" }, status: "A" }
] );
MongoDB Update
db.inventory.insertMany( [
{ item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" },
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" },
{ item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
{ 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" },
{ item: "sketchbook", qty: 80, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.5, uom: "cm" }, status: "A" }
] );
MongoDB Update
db.inventory.updateOne(
{ item: "paper" },
{
$set: { "size.uom": "cm", status: "P" },
$currentDate: { lastModified: true }
}
)
MongoDB Collection Update methods
The following methods can also add new documents to a collection:
● 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.
● db.collection.findOneAndUpdate() when used with the upsert: true option.
● db.collection.findOneAndReplace() when used with the upsert: true option.
● db.collection.save().
● db.collection.bulkWrite().
MongoDB Collection Update methods
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: "P" },
{ 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" },
] );
MongoDB Collection Update methods
db.inventory.deleteMany({ status : "A" })
The following example deletes the first document where status is "D":
db.inventory.deleteOne( { status: "D" } )
Data Modelling
Data Modelling One to One
Normalized
{
_id: "joe",
name: "Joe Bookreader"
}
{
patron_id: "joe",
street: "123 Fake Street",
city: "Faketon",
state: "MA",
zip: "12345"
}
Data Modelling One to One
Denormalized (Better Option)
{
_id: "joe",
name: "Joe Bookreader",
address: {
street: "123 Fake Street",
city: "Faketon",
state: "MA",
zip: "12345"
}
}
Data Modelling One to Many
{
_id: "joe",
name: "Joe Bookreader"
}
{
patron_id: "joe",
street: "123 Fake Street",
city: "Faketon",
state: "MA",
zip: "12345"
}
{
patron_id: "joe",
street: "1 Some Other Street",
city: "Boston",
state: "MA",
zip: "12345"
}
Data Modelling One to Many Denormalized
{
_id: "joe",
name: "Joe Bookreader",
addresses: [
{
street: "123 Fake Street",
city: "Faketon",
state: "MA",
zip: "12345"
},
{
street: "1 Some Other Street",
city: "Boston",
state: "MA",
zip: "12345"
}
]
}
Save the number of queries Advantage
Data Modelling One to Many Denormalized
Data Modelling
The Process of Data Modeling in MongoDB
Data modeling comes with improved database performance, but at the expense of some considerations
which include:
● Data retrieval patterns
● Balancing needs of the application such as: queries, updates and data processing
● Performance features of the chosen database engine
● The Inherent structure of the data itself
Data Modelling
MongoDB Document Structure
Documents in MongoDB play a major role in the decision making over which technique to apply for a
given set of data. There are generally two relationships between data, which are:
● Embedded Data
● Reference Data
Data Modelling
● In this case, related data is stored within a single document either as a field value or an array
within the document itself. The main advantage of this approach is that data is denormalized
and therefore provides an opportunity for manipulating the related data in a single database
operation. Consequently, this improves the rate at which CRUD operations are carried out,
hence fewer queries are required. Let’s consider an example of a document below
Data Modelling
{ "_id" : ObjectId("5b98bfe7e8b9ab9875e4c80c"),
"StudentName" : "George Beckonn",
"Settings" : {
"location" : "Embassy",
"ParentPhone" : 724765986
"bus" : "KAZ 450G",
"distance" : "4",
"placeLocation" : {
"lat" : -0.376252,
"lng" : 36.937389
}
}
}
Data Modelling
Strengths of Embedding
1. Increased data access speed: For an improved rate of access to data, embedding is the best
option since a single query operation can manipulate data within the specified document with
just a single database look-up.
2. Reduced data inconsistency: During operation, if something goes wrong (for example a network
disconnection or power failure) only a few numbers of documents may be affected since the
criteria often select a single document.
3. Reduced CRUD operations. This is to say, the read operations will actually outnumber the
writes. Besides, it is possible to update related data in a single atomic write operation. I.e for the
above data, we can update the phone number and also increase the distance with this single o
Data Modelling
db.students.updateOne({StudentName : "George Beckonn"}, {
$set: {"ParentPhone" : 72436986},
$inc: {"Settings.distance": 1}
})
Data Modelling
Weaknesses of Embedding
1. Restricted document size. All documents in MongoDB are constrained to the BSON size of 16
megabytes. Therefore, overall document size together with embedded data should not surpass
this limit. Otherwise, for some storage engines such as MMAPv1, data may outgrow and result
in data fragmentation as a result of degraded write performance.
2. Data duplication: multiple copies of the same data make it harder to query the replicated data
and it may take longer to filter embedded documents, hence outdo the core advantage of
embedding.
Data Modelling
Dot Notation
The dot notation is the identifying feature for embedded data in the programming part. It is used to
access elements of an embedded field or an array. In the sample data above, we can return information
of the student whose location is “Embassy” with this query using the dot notation.
db.users.find({'Settings.location': 'Embassy'})
Data Modelling
Flexible Schema
A flexible schema in MongoDB defines that the documents not necessarily need to have the same fields
or data type, for a field can differ across documents within a collection. The core advantage with this
concept is that one can add new fields, remove existing ones or change the field values to a new type
and hence update the document into a new structure.
For example we can have these 2 documents in the same collection
Data Modelling
{ "_id" : ObjectId("5b98bfe7e8b9ab9875e4c80c"),
"StudentName" : "George Beckonn",
"ParentPhone" : 75646344,
"age" : 10
}
{ "_id" : ObjectId("5b98bfe7e8b9ab98757e8b9a"),
"StudentName" : "Fredrick Wesonga",
"ParentPhone" : false,
}
Data Modelling
Example:
Let’s insert the data below in a client collection.
Data Modelling
However, validation can also be applied to already existing documents.
There are 3 levels of validation:
1. Strict: this is the default MongoDB validation level and it applies validation rules to all inserts and
updates.
2. Moderate: The validation rules are applied during inserts, updates and to already existing
documents that fulfill the validation criteria only.
3. Off: this level sets the validation rules for a given schema to null hence no validation will be done
to the documents.
Data Modelling
db.clients.insert([
{
"_id" : 1,
"name" : "Brillian",
"phone" : "+1 778 574 666",
"city" : "Beijing",
"status" : "Married"
},
{
"_id" : 2,
"name" : "James",
"city" : "Peninsula"
}
]
Data Modelling
If we apply the moderate validation level using:
db.runCommand( {
collMod: "test",
validator: { $jsonSchema: {
bsonType: "object",
required: [ "phone", "name" ],
properties: {
phone: {
bsonType: "string",
description: "must be a string and is required"
},
name: {
bsonType: "string",
description: "must be a string and is required"
}
}
} },
validationLevel: "moderate"
Data Modelling
Schema Validation Actions
After doing validation on documents, there may be some that may violate the validation rules. There is
always a need to provide an action when this happens.
MongoDB provides two actions that can be issued to the documents that fail the validation rules:
1. Error: this is the default MongoDB action, which rejects any insert or update in case it violates
the validation criteria.
2. Warn: This action will record the violation in the MongoDB log, but allows the insert or update
operation to be completed. For example:
Data Modelling
db.createCollection("students", {
validator: {$jsonSchema: {
bsonType: "object",
required: [ "name", "gpa" ],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required"
},
gpa: {
bsonType: [ "double" ],
minimum: 0,
description: "must be a double and is required"
}
}
},
validationAction: “warn”
})
Data Modelling
db.students.insert( { name: "Amanda", status: "Updated" } )
The gpa is missing regardless of the fact that it is a required field in the schema design, but since the
validation action has been set to warn, the document will be saved and an error message will be
recorded in the MongoDB log.
Validation action can not be set on admin,local and config databases.
Data Modelling
db.createCollection( "contacts5", {
validator: { $jsonSchema: {
bsonType: "object",
required: [ "phone" ],
properties: {
phone: {
bsonType: "string",
description: "must be a string and is required"
},
email: {
bsonType : "string",
pattern : "@mongodb.com$",
description: "must be a string and match the regular expression pattern"
},
status: {
enum: [ "Unknown", "Incomplete" ],
description: "can only be one of the enum values"
}
}
} },
validationAction: "error"
} )
Data Modelling
> db.contacts5.insert( { phone: "123" ,email: "abc@mongodb.com", status: "Unknown" } )
WriteResult({ "nInserted" : 1 })
> db.contacts5.insert( { phone: 123 ,email: "abc@mongodb.com", status: "Unknown" } )
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 121,
"errmsg" : "Document failed validation"
}
})
>
Data Modelling Normalization
Data Modelling DeNormalization
Data Modelling DeNormalization
The advantage of this is that you need one less query to get the information. The
downside is that it takes up more space and is more difficult to keep in sync. For
example, we decide that the light style should be renamed day. We would have to
update every single document where the user.accountsPref.style was light.
Data Modelling Join
db.order1.insert([
{ "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 },
{ "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 },
{ "_id" : 3 }
])
db.inventory1.insert([
{ "_id" : 1, "sku" : "almonds", description: "product 1", "instock" : 120 },
{ "_id" : 2, "sku" : "bread", description: "product 2", "instock" : 80 },
{ "_id" : 3, "sku" : "cashews", description: "product 3", "instock" : 60 },
{ "_id" : 4, "sku" : "pecans", description: "product 4", "instock" : 70 },
{ "_id" : 5, "sku": null, description: "Incomplete" },
{ "_id" : 6 }
])
Data Modelling Join
db.orders.aggregate([
{
$lookup:
{
from: "inventory",
localField: "item",
foreignField: "sku",
as: "inventory_docs"
}
}
])
Data Modelling Join
SQL equivalent is:
SELECT *, stockdata
FROM orders
WHERE stockdata IN (SELECT warehouse, instock
FROM warehouses
WHERE stock_item= orders.item
AND instock >= orders.ordered );
Data Modelling Nested Pipeline
db.orders2.insert([
{ "_id" : 1, "item" : "almonds", "price" : 12, "ordered" : 2 },
{ "_id" : 2, "item" : "pecans", "price" : 20, "ordered" : 1 },
{ "_id" : 3, "item" : "cookies", "price" : 10, "ordered" : 60 }
])
Data Modelling Nested Pipeline
db.warehouses.insert([
{ "_id" : 1, "stock_item" : "almonds", warehouse: "A", "instock" : 120 },
{ "_id" : 2, "stock_item" : "pecans", warehouse: "A", "instock" : 80 },
{ "_id" : 3, "stock_item" : "almonds", warehouse: "B", "instock" : 60 },
{ "_id" : 4, "stock_item" : "cookies", warehouse: "B", "instock" : 40 },
{ "_id" : 5, "stock_item" : "cookies", warehouse: "A", "instock" : 80 }
])
Data Modelling Nested Pipeline
db.orders2.aggregate([
{ $lookup:
{
from: "warehouses",
let: { order_item: "$item", order_qty: "$ordered" },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$stock_item", "$$order_item" ] },
{ $gte: [ "$instock", "$$order_qty" ] }
]
}
}
},
{ $project: { stock_item: 0, _id: 0 } }
],
as: "stockdata"
}
}
])