SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Downloaden Sie, um offline zu lesen
NoSQL & MongoDB..Part II
Arindam Chatterjee
Indexes in MongoDB
•

Indexes support the efficient resolution of queries in MongoDB.
–Without indexes, MongoDB must scan every document in a collection to select
those documents that match the query statement.
–These collection scans are inefficient and require the mongod to process a large
volume of data for each operation.

•

Indexes are special data structures that store a small portion of the
collection’s data set in an easy to traverse form.
–The index stores the value of a specific field or set of fields, ordered by the value of
the field.

•

Indexes in MongoDB are similar to indexes in other database systems.

•

MongoDB defines indexes at the collection level and supports indexes on
any field or sub-field of the documents in a MongoDB collection.
Indexes in MongoDB..2
•

The following diagram illustrates a query that selects documents using an
index.

MongoDB narrows the query by scanning the range of documents with values of score
less than 30.
Indexes in MongoDB..3
•

MongoDB can use indexes to return documents sorted by the index key
directly from the index without requiring an additional sort phase.
Descending
Indexes in MongoDB..4
Index Types
• Default _id
–All MongoDB collections have an index on the _id field that exists by default. If
applications do not specify a value for _id the driver or the mongod will create an
_id field with an ObjectID value.
–The _id index is unique, and prevents clients from inserting two documents with
the same value for the _id field.

•

Single Field
–MongoDB supports user-defined indexes on a single field of a document.
Example: Index on score filed (ascending)
Indexes in MongoDB..5
Index Types
• Compound Index
–These are user-defined indexes on multiple fields

Example: Diagram of a compound index on the userid field (ascending) and the
score field (descending). The index sorts first by the userid field and then by the
score field.
Indexes in MongoDB..6
Index Types
• Multikey Index
–MongoDB uses multikey indexes to index the content stored in arrays.
–If we index a field that holds an array value, MongoDB creates separate index
entries for every element of the array.
–These multikey indexes allow queries to select documents that contain arrays by
matching on element or elements of the arrays.
–MongoDB automatically determines whether to create a multikey index if the
indexed field contains an array value; we do not need to explicitly specify the
multikey type.
Indexes in MongoDB..7
Index Types
• Multikey Index: Illustration
Diagram of a multikey index on the addr.zip field.
The addr field contains an array of address
documents. The address documents contain the
zip field.
Indexes in MongoDB..8
Other Index Types
• Geospatial Index
–

•

Text Index
–
–

•

MongoDB provides two special indexes: 2d indexes that uses planar geometry
when returning results and 2sphere indexes that use spherical geometry to
return results.
MongoDB provides a beta text index type that supports searching for string
content in a collection.
These text indexes do not store language-specific stop words (e.g. “the”, “a”,
“or”) and stem the words in a collection to only store root words.

Hashed Index
–

To support hash based sharding, MongoDB provides a hashed index type,
which indexes the hash of the value of a field. These indexes have a more
random distribution of values along their range, but only support equality
matches and cannot support range-based queries.
Indexes in MongoDB..9
Explicit creation of Index
•

Using ensureIndex() from shell
– The following creates an index on the phone-number field of the people
collection
• db.people.ensureIndex( { "phone-number": 1 } ) .

–

The following operation will create an index on the item, category, and
price fields of the products collection
• db.products.ensureIndex( { item: 1, category: 1, price: 1 } )

–

unique constraint prevent applications from inserting documents that
have duplicate values for the inserted fields. The following example
creates a unique index on the "tax-id": of the accounts collection to
prevent storing multiple account records for the same legal entity
• db.accounts.ensureIndex( { "tax-id": 1 }, { unique: true } )

–

ensureIndex() only creates an index if an index of the same specification
does not already exist.
Indexes in MongoDB..10
Indexing Strategies
• Create Indexes to Support Your Queries
–

•

Use Indexes to Sort Query Results
–

•

To support efficient queries, use the strategies here when you specify the
sequential order and sort order of index fields.

Ensure Indexes Fit in RAM
–

•

An index supports a query when the index contains all the fields scanned
by the query. Creating indexes that supports queries results in greatly
increased query performance.

When your index fits in RAM, the system can avoid reading the index from
disk and you get the fastest processing.

Create Queries that Ensure Selectivity
–

Selectivity is the ability of a query to narrow results using the index.
Selectivity allows MongoDB to use the index for a larger portion of the
work associated with fulfilling the query.
Indexes in MongoDB..11
•

Indexes to Support Queries
–

–

For commonly issued queries, create indexes. If a query searches multiple
fields, create a compound index. Scanning an index is much faster than
scanning a collection.
Consider a posts collection containing blog posts, and if we need to regularly
issue a query that sorts on the author_name field, then we can optimize the
query by creating an index on the author_name field
• db.posts.ensureIndex( { author_name : 1 } )

–

If we regularly issue a query that sorts on the timestamp field, then we can
optimize the query by creating an index on the timestamp field
• db.posts.ensureIndex( { timestamp : 1 } )
If we want to limit the results to reduce network load, we can use limit()
• db.posts.find().sort( { timestamp : -1 } ).limit(10) [
Indexes in MongoDB..12
•

Index Administration
–
–

•

Detailed information about indexes is stored in the system.indexes collection of
each database.
system.indexes is a reserved collection, so we cannot insert documents into it
or remove documents from it. We can manipulate its documents only through
ensureIndex and the dropIndexes database command.

Running Index at Background
–

Building indexes is time-consuming and resource-intensive. Using the
{"background" : true} option builds the index in the background, while handling
incoming requests.
• > db.people.ensureIndex({"username" : 1}, {"background" : true})

–
–

If we do not include the “background” option, the database will block all other
requests while the index is being built.
Creating indexes on existing documents is faster than creating the index first
and then inserting all of the documents.
Indexes in MongoDB..12
•

Do’s and Do not’s
–

Create index only on the keys required for the query
• Indexes create additional overhead on the database
• Insert, Update and Delete operations become slow with too many idexes

–

Index direction is important if there are more than one keys
• Index with {"username" : 1, "age" : -1} and {"username" : 1, "age" : 1} have different
connotation

–
–
–

There is a built-in maximum of 64 indexes per collection, which is more than
almost any application should need.
Delete Index with “dropIndexes” if it is not required
Sometimes the most efficient solution is actually not to use an index. In general,
if a query is returning a half or more of the collection, it will be more efficient for
the database to just do a table scan instead of having to look up the index and
then the value for almost every single document.
Exercise 2
•

Insert records in collection userdetail
–
–
–
–
–
–
–
–
–
–
–

•

{"username" : "smith", "age" : 48, "user_id" : 0 }
{"username" : "smith", "age" : 30, "user_id" : 1 }
{"username" : "john", "age" : 36, "user_id" : 2 }
{"username" : "john", "age" : 18, "user_id" : 3 }
{"username" : "joe", "age" : 36, "user_id" : 4 }
{"username" : "john", "age" : 7, "user_id" : 5 }
{"username" : "simon", "age" : 3, "user_id" : 6 }
{"username" : "joe", "age" : 27, "user_id" : 7 }
{"username" : "jacob", "age" : 17, "user_id" : 8 }
{"username" : "sally", "age" : 52, "user_id" : 9 }
{"username" : "simon", "age" : 59, "user_id" : 10 }

Run the ensureIndex operation
– db.userdetail.ensureIndex({"username" : 1, "age" : -1})
Data Modelling in MongoDB
Data Modelling in MongoDB
•

MongoDB has flexible Schema unlike Relational Databases. We need not declare
Table’s schema before inserting data.

•

MongoDB’s collections do not enforce document structure

•

There are 2 ways of mapping Relationships
–References
–Embedded Documents

Example: References
• Both the “contact” and
“access” documents
contain a reference to the
“user” document.
• These are normalized
data models
Data Modelling in MongoDB..2
Example: Embedded Documents
“contact” and “access” are subdocuments embedded in main document.
This is a “denormalized” data model
Data Modelling in MongoDB..3
References vs. Embedded Documents

References: Used when

Embedded documents: Used when

• embedding would result in
duplication of data but would not
provide sufficient read
performance advantages to
outweigh the implications of the
duplication.

• we have “contains” relationships
between entities.

• to represent more complex manyto-many relationships.
• to model large hierarchical data
sets.

• we have one-to-many relationships
between entities. In these
relationships the “many” or child
documents always appear with or
are viewed in the context of the
“one” or parent documents.
• We need applications to store
related pieces of information in the
same database record.
Data Modelling in MongoDB..4
One to many relationships : Example where Embedding is advantageous
Using References

Using Embedded documents

{

{
_id: “chat",
name: "ABC Chat"

_id: "chat",
name: "ABC Chat",
addresses: [
{
street: "10 Simla Street",
city: "Kolkata",
zip: 700006
},
{
street: "132 Lanka Street",
zip: 400032
}
]

}
{
patron_id: "chat",
street: "10 Simla Street",
city: "Kolkata",
zip: 700006
}
{
patron_id: "chat",
street: "132 Lanka Street",
city: "Mumbai",
zip: 400032

}

}

Issue with above: If the application frequently
retrieves the address data with the name
information, then your application needs to
issue multiple queries to resolve the references

With the embedded data model, the application
can retrieve the complete patron information with
one query.
Data Modelling in MongoDB..5
One to many relationships : Example where referencing is advantageous
Using Embedded documents

Using Reference

{

{
title: "MongoDB: The Definitive Guide",
author: [ "Kristina Chodorow", "Mike Dirolf" ],
published_date: ISODate("2010-09-24"),
pages: 216,
language: "English",
publisher: {
name: "O'Reilly Media",
location: "CA",
}

_id: "oreilly",
name: "O'Reilly Media",
location: "CA"
}
{
_id: 123456789,
title: "MongoDB: The Definitive Guide",
author: [ "Kristina Chodorow", "Mike Dirolf" ],
published_date: ISODate("2010-09-24"),
pages: 216,
language: "English",
publisher_id: "oreilly"

}
{
title: "50 Tips and Tricks for MongoDB Developer",
author: "Kristina Chodorow",
published_date: ISODate("2011-05-06"),
pages: 68,
language: "English",
publisher: {
name: "O'Reilly Media",
location: "CA",
}

}
{
_id: 234567890,
title: "50 Tips and Tricks for MongoDB Developer",
author: "Kristina Chodorow",
published_date: ISODate("2011-05-06"),
pages: 68,
language: "English",
publisher_id: "oreilly"

}

Issue with above: Embedding leads to
repetition of publisher data.

}

Publisher Information kept separately in the
above example to avoid repetition.
Data Modelling in MongoDB..6
Tree structure with parent references
Data Modelling in MongoDB..7
Modelling Tree structure with Parent reference
•

The following lines of code describes the tree structure in previous slide
–
–
–
–
–
–

•

db.categories.insert( { _id: "MongoDB", parent: "Databases" } )
db.categories.insert( { _id: “dbm", parent: "Databases" } )
db.categories.insert( { _id: "Databases", parent: "Programming" } )
db.categories.insert( { _id: "Languages", parent: "Programming" } )
db.categories.insert( { _id: "Programming", parent: "Books" } )
db.categories.insert( { _id: "Books", parent: null } )

The query to retrieve the parent of a node
– db.categories.findOne( { _id: "MongoDB" } ).parent;

•

Query by the parent field to find its immediate children nodes
– db.categories.find( { parent: "Databases" } );
Data Modelling in MongoDB..8
Modelling Tree structure with Child reference
•

The following lines of code describes the sametree structure
db.categories.insert( { _id: "MongoDB", children: [] } );
db.categories.insert( { _id: “dbm", children: [] } );
db.categories.insert( { _id: "Databases", children: [ "MongoDB", “dbm" ] } );
db.categories.insert( { _id: "Languages", children: [] } )
db.categories.insert( { _id: "Programming", children: [ "Databases",
"Languages" ] } );
– db.categories.insert( { _id: "Books", children: [ "Programming" ] } );

–
–
–
–
–

•

The query to retrieve the immediate child of a node
– db.categories.findOne( { _id: "Databases" } ).children;

•

Query by the child field to find its parent nodes
– db.categories.find( { children: "MongoDB" } );
Data Modelling in MongoDB..8
Data Modelling for “Atomic” operations
•

Example (Online purchase portal):
– Step I: Insert data in a collection called “books” including the number of available copies
– Step II: Check if the book is available during checkout
Code
– Step I:
db.book.insert ({
_id: 123456789,
title: "MongoDB: The Definitive Guide",
author: [ "Kristina Chodorow", "Mike Dirolf" ],
published_date: ISODate("2010-09-24"),
pages: 216,
language: "English",
publisher_id: "oreilly",
available: 3,
checkout: [ { by: "joe", date: ISODate("2012-10-15") } ]
});
Data Modelling in MongoDB..9
Data Modelling for “Atomic” operations
Code
– Step II
db.book.findAndModify ( {
query: {
_id: 123456789,
available: { $gt: 0 }
},
update: {
$inc: { available: -1 },
$push: { checkout: { by: "abc", date: new Date() } }
}
} );
– In the above example, db.collection.findAndModify() method is used to atomically determine
if a book is available for checkout and update with the new checkout information.
– Embedding the available field and the checkout field within the same document ensures that
the updates to these fields are in sync:
Data Modelling in MongoDB..10
Keyword based Search
Example: Perform a keyword based search in a collection “volumes”
– Step I: Insert data in a collection “volumes”

db.volumes.insert ({
title : "Moby-Dick" ,
author : "Herman Melville" ,
published : 1851 ,
ISBN : 0451526996 ,
topics : [ "whaling" , "allegory" , "revenge" , "American" ,
"novel" , "nautical" , "voyage" , "Cape Cod" ]
});
In the above example, several topics are included on which we can perform keyword search
–

Step II: create a multi-key index on the topics array

db.volumes.ensureIndex( { topics: 1 } )
–

Step III: Search based on keyword “voyage”

• db.volumes.findOne( { topics : "voyage" }, { title: 1 } )
Exercise
•
•

Create a collection named product meant for albums. The album can have several
product types including Audio Album and Movie.
Record of Audio album can be created with the following attributes
–

–

•

Record 1 (music Album) sku (character, unique identifier), type-Audio Album ,title:”
Remembering Manna De”, description “By Music lovers”, physical_description (weight, width,
height, depth), pricing (list, retail, savings, pct_savings), details (title, artist,genre (“bengali
modern”, “bengali film”), tracks (“birth”, “childhood”, “growing up”, “end”)
Record 2 (movie) with similar details and description pertaining to movie (e.g. director, writer,
music director, actors)

Assignment
–
–

Write a query to return all products with a discount>10%
Write a query which will return the documents for the albums of a specific genre, sorted in
reverse chronological order

–

Write a query which selects films that a particular actor starred in, sorted by issue date

Weitere ähnliche Inhalte

Was ist angesagt?

MongoDB .local Munich 2019: Managing a Heterogeneous Stack with MongoDB & SQL
MongoDB .local Munich 2019: Managing a Heterogeneous Stack with MongoDB & SQLMongoDB .local Munich 2019: Managing a Heterogeneous Stack with MongoDB & SQL
MongoDB .local Munich 2019: Managing a Heterogeneous Stack with MongoDB & SQLMongoDB
 
MongoDB 2.4 and spring data
MongoDB 2.4 and spring dataMongoDB 2.4 and spring data
MongoDB 2.4 and spring dataJimmy Ray
 
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorialsMongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorialsSpringPeople
 
Enhancement of Searching and Analyzing the Document using Elastic Search
Enhancement of Searching and Analyzing the Document using Elastic SearchEnhancement of Searching and Analyzing the Document using Elastic Search
Enhancement of Searching and Analyzing the Document using Elastic SearchIRJET Journal
 
Benefits of using MongoDB: Reduce Complexity & Adapt to Changes
Benefits of using MongoDB: Reduce Complexity & Adapt to ChangesBenefits of using MongoDB: Reduce Complexity & Adapt to Changes
Benefits of using MongoDB: Reduce Complexity & Adapt to ChangesAlex Nguyen
 
Elastic 101 index operations
Elastic 101   index operationsElastic 101   index operations
Elastic 101 index operationsIsmaeel Enjreny
 
Elastic 101 - Get started
Elastic 101 - Get startedElastic 101 - Get started
Elastic 101 - Get startedIsmaeel Enjreny
 
Mongo Bb - NoSQL tutorial
Mongo Bb - NoSQL tutorialMongo Bb - NoSQL tutorial
Mongo Bb - NoSQL tutorialMohan Rathour
 
MongoDB- Crud Operation
MongoDB- Crud OperationMongoDB- Crud Operation
MongoDB- Crud OperationEdureka!
 
Top 10 frameworks of node js
Top 10 frameworks of node jsTop 10 frameworks of node js
Top 10 frameworks of node jsHabilelabs
 

Was ist angesagt? (20)

Extend db
Extend dbExtend db
Extend db
 
Mongodb By Vipin
Mongodb By VipinMongodb By Vipin
Mongodb By Vipin
 
MongoDB .local Munich 2019: Managing a Heterogeneous Stack with MongoDB & SQL
MongoDB .local Munich 2019: Managing a Heterogeneous Stack with MongoDB & SQLMongoDB .local Munich 2019: Managing a Heterogeneous Stack with MongoDB & SQL
MongoDB .local Munich 2019: Managing a Heterogeneous Stack with MongoDB & SQL
 
AngularJS By Vipin
AngularJS By VipinAngularJS By Vipin
AngularJS By Vipin
 
MongoDB 2.4 and spring data
MongoDB 2.4 and spring dataMongoDB 2.4 and spring data
MongoDB 2.4 and spring data
 
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorialsMongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
 
Enhancement of Searching and Analyzing the Document using Elastic Search
Enhancement of Searching and Analyzing the Document using Elastic SearchEnhancement of Searching and Analyzing the Document using Elastic Search
Enhancement of Searching and Analyzing the Document using Elastic Search
 
Mongo DB
Mongo DB Mongo DB
Mongo DB
 
Benefits of using MongoDB: Reduce Complexity & Adapt to Changes
Benefits of using MongoDB: Reduce Complexity & Adapt to ChangesBenefits of using MongoDB: Reduce Complexity & Adapt to Changes
Benefits of using MongoDB: Reduce Complexity & Adapt to Changes
 
Mongo DB
Mongo DBMongo DB
Mongo DB
 
Mongo DB
Mongo DBMongo DB
Mongo DB
 
Elastic 101 index operations
Elastic 101   index operationsElastic 101   index operations
Elastic 101 index operations
 
Mongo db
Mongo dbMongo db
Mongo db
 
Mongo db report
Mongo db reportMongo db report
Mongo db report
 
Elastic 101 - Get started
Elastic 101 - Get startedElastic 101 - Get started
Elastic 101 - Get started
 
Mongo Bb - NoSQL tutorial
Mongo Bb - NoSQL tutorialMongo Bb - NoSQL tutorial
Mongo Bb - NoSQL tutorial
 
Mongodb
MongodbMongodb
Mongodb
 
MongoDB- Crud Operation
MongoDB- Crud OperationMongoDB- Crud Operation
MongoDB- Crud Operation
 
Top 10 frameworks of node js
Top 10 frameworks of node jsTop 10 frameworks of node js
Top 10 frameworks of node js
 
Mongo db
Mongo dbMongo db
Mongo db
 

Ähnlich wie Nosql part 2

unit 4,Indexes in database.docx
unit 4,Indexes in database.docxunit 4,Indexes in database.docx
unit 4,Indexes in database.docxRaviRajput416403
 
Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleMongoDB
 
Mongo db a deep dive of mongodb indexes
Mongo db  a deep dive of mongodb indexesMongo db  a deep dive of mongodb indexes
Mongo db a deep dive of mongodb indexesRajesh Kumar
 
Fast querying indexing for performance (4)
Fast querying   indexing for performance (4)Fast querying   indexing for performance (4)
Fast querying indexing for performance (4)MongoDB
 
Mongo db tutorials
Mongo db tutorialsMongo db tutorials
Mongo db tutorialsAnuj Jain
 
Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26kreuter
 
MongoDB NoSQL database a deep dive -MyWhitePaper
MongoDB  NoSQL database a deep dive -MyWhitePaperMongoDB  NoSQL database a deep dive -MyWhitePaper
MongoDB NoSQL database a deep dive -MyWhitePaperRajesh Kumar
 
MongoDB and Indexes - MUG Denver - 20160329
MongoDB and Indexes - MUG Denver - 20160329MongoDB and Indexes - MUG Denver - 20160329
MongoDB and Indexes - MUG Denver - 20160329Douglas Duncan
 
Storage dei dati con MongoDB
Storage dei dati con MongoDBStorage dei dati con MongoDB
Storage dei dati con MongoDBAndrea Balducci
 
Mongodb Performance
Mongodb PerformanceMongodb Performance
Mongodb PerformanceJack
 
9. Document Oriented Databases
9. Document Oriented Databases9. Document Oriented Databases
9. Document Oriented DatabasesFabio Fumarola
 
MongoDB - An Introduction
MongoDB - An IntroductionMongoDB - An Introduction
MongoDB - An Introductiondinkar thakur
 
Indexing and Query Optimizer
Indexing and Query OptimizerIndexing and Query Optimizer
Indexing and Query OptimizerMongoDB
 
Indexing and Query Performance in MongoDB.pdf
Indexing and Query Performance in MongoDB.pdfIndexing and Query Performance in MongoDB.pdf
Indexing and Query Performance in MongoDB.pdfMalak Abu Hammad
 
MongoDB using Grails plugin by puneet behl
MongoDB using Grails plugin by puneet behlMongoDB using Grails plugin by puneet behl
MongoDB using Grails plugin by puneet behlTO THE NEW | Technology
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBRaghunath A
 
Introduction to MongoDB and its best practices
Introduction to MongoDB and its best practicesIntroduction to MongoDB and its best practices
Introduction to MongoDB and its best practicesAshishRathore72
 

Ähnlich wie Nosql part 2 (20)

unit 4,Indexes in database.docx
unit 4,Indexes in database.docxunit 4,Indexes in database.docx
unit 4,Indexes in database.docx
 
Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You Scale
 
Mongo db a deep dive of mongodb indexes
Mongo db  a deep dive of mongodb indexesMongo db  a deep dive of mongodb indexes
Mongo db a deep dive of mongodb indexes
 
Mongo db
Mongo dbMongo db
Mongo db
 
Fast querying indexing for performance (4)
Fast querying   indexing for performance (4)Fast querying   indexing for performance (4)
Fast querying indexing for performance (4)
 
Mongo db tutorials
Mongo db tutorialsMongo db tutorials
Mongo db tutorials
 
Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26
 
MongoDB NoSQL database a deep dive -MyWhitePaper
MongoDB  NoSQL database a deep dive -MyWhitePaperMongoDB  NoSQL database a deep dive -MyWhitePaper
MongoDB NoSQL database a deep dive -MyWhitePaper
 
MongoDB and Indexes - MUG Denver - 20160329
MongoDB and Indexes - MUG Denver - 20160329MongoDB and Indexes - MUG Denver - 20160329
MongoDB and Indexes - MUG Denver - 20160329
 
MongoDB_ppt.pptx
MongoDB_ppt.pptxMongoDB_ppt.pptx
MongoDB_ppt.pptx
 
Storage dei dati con MongoDB
Storage dei dati con MongoDBStorage dei dati con MongoDB
Storage dei dati con MongoDB
 
Mongodb Performance
Mongodb PerformanceMongodb Performance
Mongodb Performance
 
9. Document Oriented Databases
9. Document Oriented Databases9. Document Oriented Databases
9. Document Oriented Databases
 
MongoDB - An Introduction
MongoDB - An IntroductionMongoDB - An Introduction
MongoDB - An Introduction
 
Indexing and Query Optimizer
Indexing and Query OptimizerIndexing and Query Optimizer
Indexing and Query Optimizer
 
Indexing and Query Performance in MongoDB.pdf
Indexing and Query Performance in MongoDB.pdfIndexing and Query Performance in MongoDB.pdf
Indexing and Query Performance in MongoDB.pdf
 
MongoDB using Grails plugin by puneet behl
MongoDB using Grails plugin by puneet behlMongoDB using Grails plugin by puneet behl
MongoDB using Grails plugin by puneet behl
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Introduction to MongoDB and its best practices
Introduction to MongoDB and its best practicesIntroduction to MongoDB and its best practices
Introduction to MongoDB and its best practices
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 

Mehr von Ruru Chowdhury

The One With The Wizards and Dragons. Prelims
The One With The Wizards and Dragons. PrelimsThe One With The Wizards and Dragons. Prelims
The One With The Wizards and Dragons. PrelimsRuru Chowdhury
 
The One With The Wizards and Dragons. Finals
The One With The Wizards and Dragons. FinalsThe One With The Wizards and Dragons. Finals
The One With The Wizards and Dragons. FinalsRuru Chowdhury
 
Statr session 25 and 26
Statr session 25 and 26Statr session 25 and 26
Statr session 25 and 26Ruru Chowdhury
 
Statr session 23 and 24
Statr session 23 and 24Statr session 23 and 24
Statr session 23 and 24Ruru Chowdhury
 
Statr session 21 and 22
Statr session 21 and 22Statr session 21 and 22
Statr session 21 and 22Ruru Chowdhury
 
Statr session 19 and 20
Statr session 19 and 20Statr session 19 and 20
Statr session 19 and 20Ruru Chowdhury
 
Statr session 17 and 18
Statr session 17 and 18Statr session 17 and 18
Statr session 17 and 18Ruru Chowdhury
 
Statr session 17 and 18 (ASTR)
Statr session 17 and 18 (ASTR)Statr session 17 and 18 (ASTR)
Statr session 17 and 18 (ASTR)Ruru Chowdhury
 
Statr session 15 and 16
Statr session 15 and 16Statr session 15 and 16
Statr session 15 and 16Ruru Chowdhury
 
Statr session14, Jan 11
Statr session14, Jan 11Statr session14, Jan 11
Statr session14, Jan 11Ruru Chowdhury
 
JM Statr session 13, Jan 11
JM Statr session 13, Jan 11JM Statr session 13, Jan 11
JM Statr session 13, Jan 11Ruru Chowdhury
 
Statr sessions 11 to 12
Statr sessions 11 to 12Statr sessions 11 to 12
Statr sessions 11 to 12Ruru Chowdhury
 
Nosql part1 8th December
Nosql part1 8th December Nosql part1 8th December
Nosql part1 8th December Ruru Chowdhury
 
Statr sessions 9 to 10
Statr sessions 9 to 10Statr sessions 9 to 10
Statr sessions 9 to 10Ruru Chowdhury
 

Mehr von Ruru Chowdhury (20)

The One With The Wizards and Dragons. Prelims
The One With The Wizards and Dragons. PrelimsThe One With The Wizards and Dragons. Prelims
The One With The Wizards and Dragons. Prelims
 
The One With The Wizards and Dragons. Finals
The One With The Wizards and Dragons. FinalsThe One With The Wizards and Dragons. Finals
The One With The Wizards and Dragons. Finals
 
Statr session 25 and 26
Statr session 25 and 26Statr session 25 and 26
Statr session 25 and 26
 
Statr session 23 and 24
Statr session 23 and 24Statr session 23 and 24
Statr session 23 and 24
 
Statr session 21 and 22
Statr session 21 and 22Statr session 21 and 22
Statr session 21 and 22
 
Statr session 19 and 20
Statr session 19 and 20Statr session 19 and 20
Statr session 19 and 20
 
Statr session 17 and 18
Statr session 17 and 18Statr session 17 and 18
Statr session 17 and 18
 
Statr session 17 and 18 (ASTR)
Statr session 17 and 18 (ASTR)Statr session 17 and 18 (ASTR)
Statr session 17 and 18 (ASTR)
 
Statr session 15 and 16
Statr session 15 and 16Statr session 15 and 16
Statr session 15 and 16
 
Statr session14, Jan 11
Statr session14, Jan 11Statr session14, Jan 11
Statr session14, Jan 11
 
JM Statr session 13, Jan 11
JM Statr session 13, Jan 11JM Statr session 13, Jan 11
JM Statr session 13, Jan 11
 
Statr sessions 11 to 12
Statr sessions 11 to 12Statr sessions 11 to 12
Statr sessions 11 to 12
 
Nosql part3
Nosql part3Nosql part3
Nosql part3
 
Nosql part1 8th December
Nosql part1 8th December Nosql part1 8th December
Nosql part1 8th December
 
Statr sessions 9 to 10
Statr sessions 9 to 10Statr sessions 9 to 10
Statr sessions 9 to 10
 
R part iii
R part iiiR part iii
R part iii
 
R part II
R part IIR part II
R part II
 
Statr sessions 7 to 8
Statr sessions 7 to 8Statr sessions 7 to 8
Statr sessions 7 to 8
 
R part I
R part IR part I
R part I
 
Statr sessions 4 to 6
Statr sessions 4 to 6Statr sessions 4 to 6
Statr sessions 4 to 6
 

Kürzlich hochgeladen

Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsRommel Regala
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 

Kürzlich hochgeladen (20)

Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World Politics
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 

Nosql part 2

  • 1. NoSQL & MongoDB..Part II Arindam Chatterjee
  • 2. Indexes in MongoDB • Indexes support the efficient resolution of queries in MongoDB. –Without indexes, MongoDB must scan every document in a collection to select those documents that match the query statement. –These collection scans are inefficient and require the mongod to process a large volume of data for each operation. • Indexes are special data structures that store a small portion of the collection’s data set in an easy to traverse form. –The index stores the value of a specific field or set of fields, ordered by the value of the field. • Indexes in MongoDB are similar to indexes in other database systems. • MongoDB defines indexes at the collection level and supports indexes on any field or sub-field of the documents in a MongoDB collection.
  • 3. Indexes in MongoDB..2 • The following diagram illustrates a query that selects documents using an index. MongoDB narrows the query by scanning the range of documents with values of score less than 30.
  • 4. Indexes in MongoDB..3 • MongoDB can use indexes to return documents sorted by the index key directly from the index without requiring an additional sort phase. Descending
  • 5. Indexes in MongoDB..4 Index Types • Default _id –All MongoDB collections have an index on the _id field that exists by default. If applications do not specify a value for _id the driver or the mongod will create an _id field with an ObjectID value. –The _id index is unique, and prevents clients from inserting two documents with the same value for the _id field. • Single Field –MongoDB supports user-defined indexes on a single field of a document. Example: Index on score filed (ascending)
  • 6. Indexes in MongoDB..5 Index Types • Compound Index –These are user-defined indexes on multiple fields Example: Diagram of a compound index on the userid field (ascending) and the score field (descending). The index sorts first by the userid field and then by the score field.
  • 7. Indexes in MongoDB..6 Index Types • Multikey Index –MongoDB uses multikey indexes to index the content stored in arrays. –If we index a field that holds an array value, MongoDB creates separate index entries for every element of the array. –These multikey indexes allow queries to select documents that contain arrays by matching on element or elements of the arrays. –MongoDB automatically determines whether to create a multikey index if the indexed field contains an array value; we do not need to explicitly specify the multikey type.
  • 8. Indexes in MongoDB..7 Index Types • Multikey Index: Illustration Diagram of a multikey index on the addr.zip field. The addr field contains an array of address documents. The address documents contain the zip field.
  • 9. Indexes in MongoDB..8 Other Index Types • Geospatial Index – • Text Index – – • MongoDB provides two special indexes: 2d indexes that uses planar geometry when returning results and 2sphere indexes that use spherical geometry to return results. MongoDB provides a beta text index type that supports searching for string content in a collection. These text indexes do not store language-specific stop words (e.g. “the”, “a”, “or”) and stem the words in a collection to only store root words. Hashed Index – To support hash based sharding, MongoDB provides a hashed index type, which indexes the hash of the value of a field. These indexes have a more random distribution of values along their range, but only support equality matches and cannot support range-based queries.
  • 10. Indexes in MongoDB..9 Explicit creation of Index • Using ensureIndex() from shell – The following creates an index on the phone-number field of the people collection • db.people.ensureIndex( { "phone-number": 1 } ) . – The following operation will create an index on the item, category, and price fields of the products collection • db.products.ensureIndex( { item: 1, category: 1, price: 1 } ) – unique constraint prevent applications from inserting documents that have duplicate values for the inserted fields. The following example creates a unique index on the "tax-id": of the accounts collection to prevent storing multiple account records for the same legal entity • db.accounts.ensureIndex( { "tax-id": 1 }, { unique: true } ) – ensureIndex() only creates an index if an index of the same specification does not already exist.
  • 11. Indexes in MongoDB..10 Indexing Strategies • Create Indexes to Support Your Queries – • Use Indexes to Sort Query Results – • To support efficient queries, use the strategies here when you specify the sequential order and sort order of index fields. Ensure Indexes Fit in RAM – • An index supports a query when the index contains all the fields scanned by the query. Creating indexes that supports queries results in greatly increased query performance. When your index fits in RAM, the system can avoid reading the index from disk and you get the fastest processing. Create Queries that Ensure Selectivity – Selectivity is the ability of a query to narrow results using the index. Selectivity allows MongoDB to use the index for a larger portion of the work associated with fulfilling the query.
  • 12. Indexes in MongoDB..11 • Indexes to Support Queries – – For commonly issued queries, create indexes. If a query searches multiple fields, create a compound index. Scanning an index is much faster than scanning a collection. Consider a posts collection containing blog posts, and if we need to regularly issue a query that sorts on the author_name field, then we can optimize the query by creating an index on the author_name field • db.posts.ensureIndex( { author_name : 1 } ) – If we regularly issue a query that sorts on the timestamp field, then we can optimize the query by creating an index on the timestamp field • db.posts.ensureIndex( { timestamp : 1 } ) If we want to limit the results to reduce network load, we can use limit() • db.posts.find().sort( { timestamp : -1 } ).limit(10) [
  • 13. Indexes in MongoDB..12 • Index Administration – – • Detailed information about indexes is stored in the system.indexes collection of each database. system.indexes is a reserved collection, so we cannot insert documents into it or remove documents from it. We can manipulate its documents only through ensureIndex and the dropIndexes database command. Running Index at Background – Building indexes is time-consuming and resource-intensive. Using the {"background" : true} option builds the index in the background, while handling incoming requests. • > db.people.ensureIndex({"username" : 1}, {"background" : true}) – – If we do not include the “background” option, the database will block all other requests while the index is being built. Creating indexes on existing documents is faster than creating the index first and then inserting all of the documents.
  • 14. Indexes in MongoDB..12 • Do’s and Do not’s – Create index only on the keys required for the query • Indexes create additional overhead on the database • Insert, Update and Delete operations become slow with too many idexes – Index direction is important if there are more than one keys • Index with {"username" : 1, "age" : -1} and {"username" : 1, "age" : 1} have different connotation – – – There is a built-in maximum of 64 indexes per collection, which is more than almost any application should need. Delete Index with “dropIndexes” if it is not required Sometimes the most efficient solution is actually not to use an index. In general, if a query is returning a half or more of the collection, it will be more efficient for the database to just do a table scan instead of having to look up the index and then the value for almost every single document.
  • 15. Exercise 2 • Insert records in collection userdetail – – – – – – – – – – – • {"username" : "smith", "age" : 48, "user_id" : 0 } {"username" : "smith", "age" : 30, "user_id" : 1 } {"username" : "john", "age" : 36, "user_id" : 2 } {"username" : "john", "age" : 18, "user_id" : 3 } {"username" : "joe", "age" : 36, "user_id" : 4 } {"username" : "john", "age" : 7, "user_id" : 5 } {"username" : "simon", "age" : 3, "user_id" : 6 } {"username" : "joe", "age" : 27, "user_id" : 7 } {"username" : "jacob", "age" : 17, "user_id" : 8 } {"username" : "sally", "age" : 52, "user_id" : 9 } {"username" : "simon", "age" : 59, "user_id" : 10 } Run the ensureIndex operation – db.userdetail.ensureIndex({"username" : 1, "age" : -1})
  • 16. Data Modelling in MongoDB
  • 17. Data Modelling in MongoDB • MongoDB has flexible Schema unlike Relational Databases. We need not declare Table’s schema before inserting data. • MongoDB’s collections do not enforce document structure • There are 2 ways of mapping Relationships –References –Embedded Documents Example: References • Both the “contact” and “access” documents contain a reference to the “user” document. • These are normalized data models
  • 18. Data Modelling in MongoDB..2 Example: Embedded Documents “contact” and “access” are subdocuments embedded in main document. This is a “denormalized” data model
  • 19. Data Modelling in MongoDB..3 References vs. Embedded Documents References: Used when Embedded documents: Used when • embedding would result in duplication of data but would not provide sufficient read performance advantages to outweigh the implications of the duplication. • we have “contains” relationships between entities. • to represent more complex manyto-many relationships. • to model large hierarchical data sets. • we have one-to-many relationships between entities. In these relationships the “many” or child documents always appear with or are viewed in the context of the “one” or parent documents. • We need applications to store related pieces of information in the same database record.
  • 20. Data Modelling in MongoDB..4 One to many relationships : Example where Embedding is advantageous Using References Using Embedded documents { { _id: “chat", name: "ABC Chat" _id: "chat", name: "ABC Chat", addresses: [ { street: "10 Simla Street", city: "Kolkata", zip: 700006 }, { street: "132 Lanka Street", zip: 400032 } ] } { patron_id: "chat", street: "10 Simla Street", city: "Kolkata", zip: 700006 } { patron_id: "chat", street: "132 Lanka Street", city: "Mumbai", zip: 400032 } } Issue with above: If the application frequently retrieves the address data with the name information, then your application needs to issue multiple queries to resolve the references With the embedded data model, the application can retrieve the complete patron information with one query.
  • 21. Data Modelling in MongoDB..5 One to many relationships : Example where referencing is advantageous Using Embedded documents Using Reference { { title: "MongoDB: The Definitive Guide", author: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", publisher: { name: "O'Reilly Media", location: "CA", } _id: "oreilly", name: "O'Reilly Media", location: "CA" } { _id: 123456789, title: "MongoDB: The Definitive Guide", author: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", publisher_id: "oreilly" } { title: "50 Tips and Tricks for MongoDB Developer", author: "Kristina Chodorow", published_date: ISODate("2011-05-06"), pages: 68, language: "English", publisher: { name: "O'Reilly Media", location: "CA", } } { _id: 234567890, title: "50 Tips and Tricks for MongoDB Developer", author: "Kristina Chodorow", published_date: ISODate("2011-05-06"), pages: 68, language: "English", publisher_id: "oreilly" } Issue with above: Embedding leads to repetition of publisher data. } Publisher Information kept separately in the above example to avoid repetition.
  • 22. Data Modelling in MongoDB..6 Tree structure with parent references
  • 23. Data Modelling in MongoDB..7 Modelling Tree structure with Parent reference • The following lines of code describes the tree structure in previous slide – – – – – – • db.categories.insert( { _id: "MongoDB", parent: "Databases" } ) db.categories.insert( { _id: “dbm", parent: "Databases" } ) db.categories.insert( { _id: "Databases", parent: "Programming" } ) db.categories.insert( { _id: "Languages", parent: "Programming" } ) db.categories.insert( { _id: "Programming", parent: "Books" } ) db.categories.insert( { _id: "Books", parent: null } ) The query to retrieve the parent of a node – db.categories.findOne( { _id: "MongoDB" } ).parent; • Query by the parent field to find its immediate children nodes – db.categories.find( { parent: "Databases" } );
  • 24. Data Modelling in MongoDB..8 Modelling Tree structure with Child reference • The following lines of code describes the sametree structure db.categories.insert( { _id: "MongoDB", children: [] } ); db.categories.insert( { _id: “dbm", children: [] } ); db.categories.insert( { _id: "Databases", children: [ "MongoDB", “dbm" ] } ); db.categories.insert( { _id: "Languages", children: [] } ) db.categories.insert( { _id: "Programming", children: [ "Databases", "Languages" ] } ); – db.categories.insert( { _id: "Books", children: [ "Programming" ] } ); – – – – – • The query to retrieve the immediate child of a node – db.categories.findOne( { _id: "Databases" } ).children; • Query by the child field to find its parent nodes – db.categories.find( { children: "MongoDB" } );
  • 25. Data Modelling in MongoDB..8 Data Modelling for “Atomic” operations • Example (Online purchase portal): – Step I: Insert data in a collection called “books” including the number of available copies – Step II: Check if the book is available during checkout Code – Step I: db.book.insert ({ _id: 123456789, title: "MongoDB: The Definitive Guide", author: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", publisher_id: "oreilly", available: 3, checkout: [ { by: "joe", date: ISODate("2012-10-15") } ] });
  • 26. Data Modelling in MongoDB..9 Data Modelling for “Atomic” operations Code – Step II db.book.findAndModify ( { query: { _id: 123456789, available: { $gt: 0 } }, update: { $inc: { available: -1 }, $push: { checkout: { by: "abc", date: new Date() } } } } ); – In the above example, db.collection.findAndModify() method is used to atomically determine if a book is available for checkout and update with the new checkout information. – Embedding the available field and the checkout field within the same document ensures that the updates to these fields are in sync:
  • 27. Data Modelling in MongoDB..10 Keyword based Search Example: Perform a keyword based search in a collection “volumes” – Step I: Insert data in a collection “volumes” db.volumes.insert ({ title : "Moby-Dick" , author : "Herman Melville" , published : 1851 , ISBN : 0451526996 , topics : [ "whaling" , "allegory" , "revenge" , "American" , "novel" , "nautical" , "voyage" , "Cape Cod" ] }); In the above example, several topics are included on which we can perform keyword search – Step II: create a multi-key index on the topics array db.volumes.ensureIndex( { topics: 1 } ) – Step III: Search based on keyword “voyage” • db.volumes.findOne( { topics : "voyage" }, { title: 1 } )
  • 28. Exercise • • Create a collection named product meant for albums. The album can have several product types including Audio Album and Movie. Record of Audio album can be created with the following attributes – – • Record 1 (music Album) sku (character, unique identifier), type-Audio Album ,title:” Remembering Manna De”, description “By Music lovers”, physical_description (weight, width, height, depth), pricing (list, retail, savings, pct_savings), details (title, artist,genre (“bengali modern”, “bengali film”), tracks (“birth”, “childhood”, “growing up”, “end”) Record 2 (movie) with similar details and description pertaining to movie (e.g. director, writer, music director, actors) Assignment – – Write a query to return all products with a discount>10% Write a query which will return the documents for the albums of a specific genre, sorted in reverse chronological order – Write a query which selects films that a particular actor starred in, sorted by issue date