Introduction to Schema
Considerations for NoSQL
This presentation will discuss schema considerations for NoSQL, specifically in
regard to relational schema design and MongoDB schema design. We will also
explore data types supported by MongoDB and the best practices for modeling
data for atomic operations.
by Aravinda A kumar
Relational Schema Design vs NoSQL Schema
Design
NoSQL Schema Design
Flexible, dynamic collections with denormalized data to optimize for
quick and easy retrieval
1 2
Relational Schema Design
Structured, rigid tables with many foreign keys to enforce data
integrity and consistency
Read-Write Ratio and MongoDB Schema
Design
Understanding Read-Write Ratio MongoDB Schema Design
Helps determine the best schema design to optimize Must account for indexes, sharding, and other
for either reads or writes based on the workload. performance considerations to ensure scalability and
efficiency.
Embedding vs Referencing in MongoDB
Schema Design
Embedding Referencing
Simplifies queries and ensures data locality, but Reduces duplication, but requires many queries
can lead to duplication and inconsistency. and increases complexity.
Data Types Comparison and Best Practices
1 2 3
Document Data Types Comparison Best Practices
Arrays, Numbers, Dates, Must consider storage size, Choose the simplest data type
Strings, Object,TimeStamp query performance, ease of that will suit your needs and
and ObjectID use, index support, and other avoid arrays of arrays or deep
factors. nesting.
String (String):
Represents text data
Example: "John Doe"
Integer (NumberInt):
Represents a 32-bit signed integer
Example: 42
Double (NumberDecimal):
Represents a 64-bit floating-point number
Example: 3.14159
Boolean (Boolean):
Represents true or false values
Example: true
Date (Date):
Represents a date and time
Example: ISODate("2023-09-06T10:00:00Z")
Array (Array):
Represents an ordered list of values
Example: ["apple", "banana", "cherry"]
Object (Object):
Represents a document or subdocument with key-value pairs
Null: Represents a null or missing value.
Example: "field1": null
Regular Expression: Used to store regular expressions.
Example: "pattern": /example/i
Binary Data (BinData): Stores binary data, such as images or files.
Example: "binaryData": BinData(0, "base64_encoded_data")
Decimal128: Represents arbitrary-precision decimal numbers.
Example: "price": NumberDecimal("19.99")
Symbol: Represents symbolic string data.
Example: "currency": Symbol("USD")
MinKey and MaxKey: Special values representing the smallest and largest possible BSON elements,
respectively.
Modeling Data for Atomic Operations in
MongoDB
Atomic Operations Using MongoDB Shell
Ability to perform multiple operations as a single, Code examples showing how to use the atomic
indivisible transaction to ensure consistency. operators like $inc, $push, $pull,$set and $unset
Code snippets for update(),
findandmodify() and remove()
methods
1 Update() 2 findandmodify()
db.collection.update({query}, db.collection.findAndModify({q
{update},{options}) - example: uery},{sort},{update},{options})
db.inventory.update({category: - example:
"appliances"},{$set:{quantity: db.products.findAndModify({qu
20}},{multi:true}) ery: {type: "tablet"}},{update:
{$inc: {stock: -1}}})
3 remove()
db.collection.remove({query},{options}) - example: db.orders.remove({status:
"cancelled"})