Webinar: How Penton Uses MongoDB As an Analytics Platform within their Drupal Digital Platform
MongoDB & Drupal
-
Upload
- -
Category
Technology
-
view
1.447 -
download
2
Transcript of MongoDB & Drupal
Pavel [email protected]
1.MySQL as standard.
2. Problems MySQL
3. Reply by MongoDB. Structure
4. Plus and Cons Mongo
5. Mongo and Drupal. Let's be friends
6. Work with users. Custom Module
7. Aggregation Framework
Plan
2/29MongoDB & Drupal
MySQL as standard
3/29MongoDB & Drupal
MySQL as standard
1. It is a reliable product
2. It is easy and convenient
3.A description of 90% of all possible data
storage structures *
4. It is used by millions of people *
* rough estimates
4/29MongoDB & Drupal
node_load – SLOW
user_load – SLOW
JOIN - SLOW
MySQL is SLOW
5/29MongoDB & Drupal
The choice between a set of tables or columns empty
1.Slow selection
2.Waste of memory
MySQL - hard structuring
6/29MongoDB & Drupal
Reply by MongoDB
1.Very fast insert
2.No joins => Fast select
3. Power Update
7/29MongoDB & Drupal
Structure MySQL
8/29MongoDB & Drupal
Structure Mongo
{ name: "Jhon", address:[ {city: "Sevastopol", country: "Ukraine"}, {city: "New York", country: "USA"} ], friends:[ {id: 3}, {id: 13} ], email: "[email protected]", phone: "+380123456789"}
9/29MongoDB & Drupal
Mongo Plus
1. Flexibility
2. Scalability
3. Atomicity
4. Support for various
types of data
5. Object Query Language
6. Map/Reduce
10/29MongoDB & Drupal
Mongo Minuses
1. Enough young product
2. Max object size - 4 Mb
3. Max DB size - 2 GB
4. Typing
5. No transaction
11/29MongoDB & Drupal
Mongo and PHP
Installing from a Pecl repORDownload the library from Githab and make
Detailed documentation on PHP.net and living examples in the test folder inside the library
12/29MongoDB & Drupal
Mongo and Drupal.Let's be friends
13/29MongoDB & Drupal
Setting
#MongoDB $conf['mongodb_connections'] = array( 'default' => array( // Connection name/alias 'host' => 'localhost', // Omit USER:PASS@
'db' => 'drup_conf' // Database name. ), );$conf['field_storage_default'] = 'mongodb_field_storage';
14/29MongoDB & Drupal
Modules
15/29MongoDB & Drupal
Update
16/29MongoDB & Drupal
function dc_mongodb_update($collection_name, $keys, $object, $upsert = TRUE) {
// Select collection $collection = mongodb_collection($collection_name);
if (!$collection || !$keys || !$object) { return FALSE; }
$result = $collection->update($keys, $object, array('upsert' => $upsert)); return $result;}
Find
17/29MongoDB & Drupal
function dc_mongodb_select($collection_name, $query = array(), $fields = array(), $sort = array(), $limit = 0) {
// Select collection $collection = mongodb_collection($collection_name); // Set query $mongo_result = $collection->find($query, $fields); $mongo_result->sort($sort); // Set limit if defined if ($limit > 0) { $mongo_result->limit($limit); } $result = array(); while($item = $mongo_result->getNext()) { $result[] = $item; }return $result;}
Description task
Each user can be:- name- surname- gender- post- any number of addresses- Friends List
First and last name - required Another fields - optional
18/29MongoDB & Drupal
Structure - alone
Users{ "uid" : 5, "first_name" : "Jon", "last_name" : "Smit", "gender " : "male", "post" : "manager", }
Friends{ "users" : [ { "uid" : 1 }, { "uid" : 7 } ]}
Address{ "uid" : 5, "address" : [ { "country" : "USA", "city" : "New York", "address" : "st Jimmy street 4 " }, { "country" : "Great Brithan", "city" : "London", "address" : "Queen palace" } ]}
19/29MongoDB & Drupal
The structure - all-inclusiveUsers{ "uid" : 5, "first_name" : "Jon", "last_name" : "Smit", “gender" : "male", "post" : "manager", "address" : [ { "country" : "USA", ... } ] "friends" : [ { "first_name" : "Jon", "last_name" : "Smit", “gender " : "male", "post" : "manager", "address" : [ { ... } ] }]}
20/29MongoDB & Drupal
Structure - mixed
{ "uid" : 5, "profile" : { "first_name" : "Jon", "last_name" : "Smit", "gender" : "male", "post" : "manager", "address" : [ {…}, {…} ] }, "friends" : [
{ "uid" : 1, "name" : "Bobby”},
{ "uid" : 7, "name" : "Jynu“ } ]}
21/29MongoDB & Drupal
hook_user_update
22/29MongoDB & Drupal
function dc_mongodb_user_user_update(&$edit, $account, $category){ $keys = array('uid' => (int) $account->uid); //Get user data from mongodb $data = dc_mongodb_select_one('users', $keys); $userOldName = $data['profile']['first_name'] ; //Set update data $data['uid'] = (int) $account->uid; $data['profile']['first_name'] = $edit['first_name'];//Update user collection dc_mongodb_update('users', $keys, $data); if ($userOldName != $edit['first_name']) { dc_mongodb_update('users', array('friends.uid' => $data['uid'] ),
array('$set' => array('friends.$.name' => $edit['first_name'])), FALSE); }}
Aggregation Framework
1. Means to calculate aggregated values without having to use map-reduce
2. Provides similar functionality to GROUP BY and related SQL operators
3. Add computed fields
4. Create new virtual sub-objects
5. Extract sub-fields into the top-level of results
23/29MongoDB & Drupal
Aggregation wrapper/** * Mongo aggregate */function dc_mongodb_aggregate($collection_name, $opt) { // Select collection $collection = mongodb_collection($collection_name); if (!$collection) { return FALSE; } $result = $collection->aggregate($opt); unset($collection);
return $result;}
24/29MongoDB & Drupal
Extract sub-fields
$top_users = dc_mongodb_aggregate(
‘users’, array( array('$unwind' => '$friends'), array( '$project' => array( 'uid' => 1, '_id' => 0, 'friend_uid' => '$friends.uid', 'friend_name' => '$friends.name', ) ));
{ "uid" : 1, "friend_name" : “Bob", "frined_id" : 5},...{ "uid" : 1, "friend_name" : “Alex", "frined_id" : 12}
25/29MongoDB & Drupal
Add virtual field
array( '$project' => array(
... 'count' => array( '$add' => 1 ) ))
26/29MongoDB & Drupal
{ "uid" : 1, "friend_name" : “Bob", "frined_id" : 5, "count " : 1},...{ "uid" : 1, "friend_name" : “Alex", "frined_id" : 12, "count" : 1}
Group
array( '$group' => array( '_id' => array( 'user_name' => '$friend_name', 'user_id' => '$friend_uid', ), 'count' => array( '$sum' => '$count' ) ) ),
27/29MongoDB & Drupal
{ "_id " : { "user_name " : “Bob", "user_id " : 5 }, "count " : 3},...{ "_id " : { "user_name " : “Alex", "user_id " : 2 }, "count " : 12}
Add sort and limit
array( '$sort' => array(
'count' => -1 ) ), array( $limit' => 1 )
28/29MongoDB & Drupal
{ "_id " : { "user_name " : “Alex", "user_id " : 2 }, "count " : 12}