Introduction To Schema Considerations For NoSQL
Introduction To Schema Considerations For NoSQL
by Aravinda A kumar
Relational Schema Design vs NoSQL Schema
Design
NoSQL Schema Design
1 2
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
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):
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):
MinKey and MaxKey: Special values representing the smallest and largest possible BSON elements,
respectively.
Modeling Data for Atomic Operations in
MongoDB
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()