By Ronak Raithatha
Gun to the head by Alex.
MongoDB :
MongoDB is Web Scale
Table | Collection |
Row | Document |
Column | Field |
Joins | Embedded documents/Linking |
Database | Database |
INSERT INTO seeker (fn, ln)
VALUES ('John', 'Doe')
db.seeker.insertOne({
fn: 'John',
ln: 'Doe'
})
{
"_id":ObjectId("5a5d957671775ae0f046e3e6"),
"fn":"John",
"ln":"Doe"
}
Result:
To insert multiple documents, use:
.insertMany(<document>,<document>,...)
SQL:
MongoDB:
db.seeker.insert({_id:'Stephane',country:'US'})
12 byte long:
SQL:
UPDATE SEEKER SET ln='Smith' WHERE ln='Doe'
MongoDB:
db.seeker.update(
{ln: 'Doe'},
{$set:
{ln: 'Smith'}
}
)
Replace a full document with:
.replaceOne(<filter>,<replacement>)
db.seeker.update(
{ln: 'Doe'},
{$set:
{age: 23}
}
)
Adding new field
Update multiple documents with:
.updateMany(<filter>,<update>)
SQL:
DELETE FROM seeker WHERE fn='John'
MongoDB:
db.seeker.deleteOne({fn:'John'})
SQL:
DROP TABLE seeker
MongoDB:
db.seeker.drop()
To delete many, use:
.deleteMany(<filter>)
SQL:
SELECT *
FROM seeker
MongoDB:
db.seeker.find()
SELECT id,
fn,
ln
FROM seeker
db.people.find(
{ },
{ fn: 1, ln: 1 }
)
SELECT fn, ln
FROM seeker
WHERE ln = "Doe"
db.seeker.find(
{ ln: "Doe" },
{ fn: 1, ln: 1, _id: 0 }
)
SELECT *
FROM seeker
WHERE age > 25
AND age <= 50
db.seeker.find(
{ age: { $gt: 25, $lte: 50 } }
)
SQL:
SELECT *
FROM seeker
WHERE age < 30
ORDER BY age ASC
MongoDB:
db.seeker.find( { age: { $lt: 30 } } )
.sort( { age: 1 } )
SELECT *
FROM seeker
WHERE ln like "%Do%"
db.people.find( { user_id: /bc/ } )
SELECT COUNT(*)
FROM seeker
db.people.count()
or
db.people.find().count()
SELECT *
FROM seeker
LIMIT 5
SKIP 10
db.people.find().limit(5).skip(10)
There are a couple other combined methods to work on single reads:
Provides a way to process and return data from multiple results and perform various operations on the return data.
i.e. GROUP BY, SORT, HAVING, etc.
Two most prominent ways to use the aggregation framework are:
SQL:
SELECT seeker_id,
SUM(applications) AS total
FROM seeker
GROUP BY seeker_id
ORDER BY total
MongoDB:
db.seeker.aggregate( [
{
$group: {
_id: "$seeker_id",
total: { $sum: "$applications" }
}
},
{ $sort: { total: 1 } }
] )
SELECT seeker_id,
count(*)
FROM seeker
GROUP BY seeker_id
HAVING count(*) > 1
db.seeker.aggregate( [
{
$group: {
_id: "$seeker_id",
count: { $sum: 1 }
}
},
{ $match: { count: { $gt: 1 } } }
] )
SQL:
MongoDB:
SELECT seeker_id,
app_date,
SUM(applications) AS total
FROM orders
GROUP BY seeker_id,
app_date
HAVING total > 3
db.seeker.aggregate( [
{
$group: {
_id: {
cust_id: "$seeker_id",
app_date: {
month: { $month: "$app_date" },
day: { $dayOfMonth: "$app_date" },
year: { $year: "$app_date"}
}
},
total: { $sum: "$applications" }
}
},
{ $match: { total: { $gt: 3 } } }
] )
SQL:
MongoDB:
SELECT seeker_id,
app_date,
SUM(applications) AS total
FROM orders
GROUP BY seeker_id,
app_date
HAVING total > 3
db.seeker.aggregate( [
{
$group: {
_id: {
cust_id: "$seeker_id",
app_date: {
month: { $month: "$app_date" },
day: { $dayOfMonth: "$app_date" },
year: { $year: "$app_date"}
}
},
total: { $sum: "$applications" }
}
},
{ $match: { total: { $gt: 3 } } }
] )
SQL:
MongoDB:
SELECT *, applications
FROM seeker
WHERE ethnicity_id IN (SELECT *
FROM ethnicity
WHERE id= seeker.ethnicity_id);
db.seeker.aggregate([
{
$lookup:
{
from: "ethnicity",
localField: "ethnicity_id",
foreignField: "id",
as: "ethnicity_id"
}
}
])
MongoDB provides built-in asynchronous replication by reading oplog to the master.
Maintains a heartbeat connection between the replica sets in case primary goes down.
If primary does go down, mongo determines the next primary either by priority set to each set, or hosting an election if the priority is the same.
Provides a way to horizontally scale data across multiple machines by adding a subset of the overall data to each machine.
The shard key consists of an immutable field of fields that exist in every document in a target collection
With enough interest, we can spend a hack-day working on two datasets that are similar between SQL and Mongo.
Divide up into teams and try to replicate one team's SQL query to Mongo.