3. NoSQL | What
⢠NoSQL â SQL is dead
⢠Not an opposite, but an alternative/complement to SQL (Not Only SQL)
⢠Came as a need for large volumes of data and high transaction rates
⢠Are generally based on key/value store
⢠Are very happy with denormalized data since they have no joins
Friday, April 12, 13
4. NoSQL | Why
⢠Flexible Data Model, so no prototyping is needed
⢠Scaling out instead of Scaling up
⢠Performance is significantly higher
⢠Cheaper licenses (or free)
⢠Runs on commodity hardware
⢠Caching layer already there
⢠Data variety
Friday, April 12, 13
5. NoSQL | Why not
⢠well, itâs not all ACID...
⢠Less mature than the relational systems (so the ecosystem of tools/addons
is still small)
⢠BI & Reporting is limited
⢠Coarse grained Security settings
Friday, April 12, 13
6. NoSQL | Which
⢠Key/Value stores
⢠Document Databases
⢠And more...
Friday, April 12, 13
7. MongoDB | Documents & Collections
⢠Document is the equivalent of an SQL table row and is a set of key/value
pairs
⢠Collection is the equivalent of an SQL table and is a set of documents not
necessarily of the same type
⢠Database is the equivalent of a... database
{
ânameâ : âSpyros Passasâ,
âcompanyâ : âNeyboxâ
}
{
ânameâ : âSpyros Passasâ,
âcompanyâ : âNeyboxâ
}
{
âEventâ : âJoomlaFrappeâ,
âlocationâ : âAthensâ
}
Friday, April 12, 13
8. MongoDB | What can a value be?
⢠Keys are always strings (without . and $)
⢠Value can be
⢠String
⢠Number
⢠Date
⢠Array
⢠Document
{ânameâ : âSpyros Passasâ}
{âageâ : 30}
{âbirthdayâ : Date(â1982-12-12â}
{âinterestsâ : [âProgrammingâ, âNoSQLâ]}
{âaddressâ : {
âstreetâ : â123 Pireus st.â,
âcityâ : âAthensâ,
âzip_codeâ : 17121
}
}
Friday, April 12, 13
9. MongoDB | Example of a document
{
â_idâ : ObjectId(â47cc67093475061e3d95369dâ),
ânameâ : âSpyros Passasâ,
âbirthdayâ : Date(â1982-12-12â),
âageâ : 30,
âinterestsâ : [âProgrammingâ, âNoSQLâ],
âaddressâ : {
âstreetâ : â123 Pireus st.â,
âcityâ : âAthensâ,
âzip_codeâ : 17121
}
â_idâ : ObjectId(â47cc67093475061e3d95369dâ)
ObjectId is a special type
Friday, April 12, 13
10. MongoDB | Indexes
⢠Any field can be indexed
⢠Indexes are ordered
⢠Indexes can be unique
⢠Compound indexes are possible (and in fact very useful)
⢠Can be created or dropped at anytime
⢠Indexes have a large size and an insertion overhead
Friday, April 12, 13
12. MongoDB | Data Relations
⢠MongoDB has no joins (but you can fake them in the application level)
⢠MongoDB supports nested data (and itâs a pretty good idea actually!)
⢠Collections are not necessary, but greatly help data organization and
performance
Friday, April 12, 13
13. MongoDB | Going from relational to NoSQL
⢠Rethink your data and select a proper database
⢠Rethink the relationships between your data
⢠Rethink your query access patterns to create eďŹcient indexes
⢠Get to know your NoSQL database (and its limitations)
⢠Move logic from data to application layer (but be careful)
Friday, April 12, 13
16. MongoDB | Replica Set
Primary (Master)
Secondary (Slave)
Friday, April 12, 13
17. MongoDB | Replica Set
Primary (Master)
Secondary (Slave)Secondary (Slave) Secondary (Slave)
Friday, April 12, 13
18. MongoDB | Replica Set when things go wrong
Primary (Master)
Secondary (Slave)Secondary (Slave) Secondary (Slave)
Friday, April 12, 13
19. MongoDB | Replica Set when things go wrong
Primary (Master)
Secondary (Slave) Secondary (Slave)
Friday, April 12, 13
20. MongoDB | Replica Set when things go wrong
Primary (Master)
Secondary (Slave) Secondary (Slave)Secondary (Slave)
Friday, April 12, 13
21. MongoDB | Replica set tips
⢠Physical machines should be in independent availability zones
⢠Selecting to read from slaves significantly increases performance (but you
have to be cautious)
Friday, April 12, 13
24. MongoDB | Sharding with replica sets
A...J K....P Q....Z
Replica SetReplica SetReplica Set
Friday, April 12, 13
25. MongoDB | Sharding with replica sets
A...J K....P Q....Z
Config Servers
Replica SetReplica SetReplica Set
Friday, April 12, 13
26. MongoDB | Things to consider when sharding
⢠Picking the right sharding key is of paramount importance!
⢠Rule of thumb:âthe shard key must distribute reads and writes and keep the
data youâre using togetherâ
⢠Key must be of high cardinality
⢠Key must not be monotonically ascending to infinity
⢠Key must not be random
⢠A good idea is a coarsely ascending field + a field you query a lot
Friday, April 12, 13
27. MongoDB | PHP | The driver
⢠Serializes objects to BSON
⢠Uses exceptions to handle errors
⢠Core classes
⢠MongoClient: Creates and manages DB connections
⢠MongoDB: Interact with a database
⢠MongoCollection: Represents and manages a collection
⢠MongoCursor: Used to iterate through query results
Friday, April 12, 13
28. MongoDB | PHP | MongoClient
<?php
// Gets the client
$mongo = new MongoClient(âmongodb://localhost:27017â);
// Sets the read preferences (Primary only or primary & secondary)
$mongo->setReadPreference(MongoClient::RP_SECONDARY);
// If in replica set, returns hosts status
$hosts_array = mongo->getHosts();
// Returns an array with the database names
$db_array = $mongo->listDBs();
// Returns a MongoDB object
$database = $mongo->selectDB(âmyblogâ);
?>
Creates a connection and sets read preferences
Provide info about hosts status and health
Lists, selects or drops databases
Friday, April 12, 13
29. MongoDB | PHP | MongoDB
<?php
// Create a collection
$database->createCollection(âblogpostsâ);
// Select a collection
$blogCollection = $database->selectCollection(âblogpostsâ);
// Drop a collection
$database->dropCollection(âblogpostsâ)
?>
Handles Collections
Friday, April 12, 13
31. MongoDB | PHP | MongoCollection | Update
<?php
// Update
$c->insert(array("firstname" => "Spyros", "lastname" => "Passas" ));
$newdata = array('$set' => array("address" => "123 Pireos st"));
$c->update(array("firstname" => "Spyros"), $newdata);
// Upsert
$c->update(
    array("uri" => "/summer_pics"),
    array('$inc' => array("page_hits" => 1)),
    array("upsert" => true)
);
?>
Friday, April 12, 13
33. MongoDB | PHP | MongoCollection | Query
<?php
// Get the collection
$posts = $mongo->selectDB(âblogâ)->selectCollection(âpostsâ);
// Find one
$post = $posts->findOne(array('author' => 'john'), array('title'));
// Find many
$allPosts = $posts->find(array('author'Â =>Â 'john'));
// Find using operators
$commentedPosts = $posts->find(array(âcomment_countâ => array(â$gtâ=>1)));
// Find in arrays
$tags = array(âtechnologyâ, ânosqlâ);
// Find any
$postsWithAnyTag = $posts->find(array('tags' => array('$in' => $tags)));
// Find all
$postsWithAllTags = $posts->find(array('tags' => array('$all' => $tags)));
?>
Friday, April 12, 13
34. MongoDB | PHP | MongoCursor
<?php
// Iterate through results
$results = $collection->find();
foreach ($results as $result) {
    // Do something here
}
// Sort
$posts = $posts->sort(array('created_at'=> -1));
// Skip a number of results
$posts = $posts->skip(5);
// Limit the number of results
$posts = $posts->limit(10);
// Chaining
$posts->sort(array('created_at'=> -1))->skip(5)->limit(10);
?>
Friday, April 12, 13
35. MongoDB | PHP | Query monitoring & Optimization
explain()
Gives data about index performance for a specific query
{
"n" : <num>, /* Number documents that match the query */
"nscannedObjects" : <num>, /* total number of documents scanned during
the query */
"nscanned" : <num>, /* total number of documents and index entries */
"millis" : <num>, /* time to complete the query in milliseconds */
âmillisShardTotalâ : <num> /* total time to complete the query on shards
*/
âmillisShardAvgâ : <num> /* average time to complete the query on each
shard */
}
Friday, April 12, 13