MongoDB & Drupal
Upcoming SlideShare
Loading in...5
×
 

MongoDB & Drupal

on

  • 1,355 views

 

Statistics

Views

Total Views
1,355
Views on SlideShare
1,204
Embed Views
151

Actions

Likes
0
Downloads
19
Comments
0

1 Embed 151

http://localhost 151

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    MongoDB & Drupal MongoDB & Drupal Presentation Transcript

    • Pavel Gorbachpavel.gorbach@volcanoideas.com
    • MongoDB & Drupal 2/29 Plan 1. MySQL as standard. 2. Problems MySQL 3. Reply by MongoDB. Structure 4. Plus and Cons Mongo 5. Mongo and Drupal. Lets be friends 6. Work with users. Custom Module 7. Aggregation Framework
    • MongoDB & Drupal 3/29 MySQL as standard
    • MongoDB & Drupal 4/29 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
    • MongoDB & Drupal 5/29 MySQL is SLOW node_load – SLOW user_load – SLOW JOIN - SLOW
    • MongoDB & Drupal 6/29 MySQL - hard structuring The choice between a set of tables or columns empty 1. Slow selection 2. Waste of memory
    • MongoDB & Drupal 7/29 Reply by MongoDB 1. Very fast insert 2. No joins => Fast select 3. Power Update
    • MongoDB & Drupal 8/29 Structure MySQL
    • MongoDB & Drupal 9/29 Structure Mongo { name: "Jhon", address:[ {city: "Sevastopol", country: "Ukraine"}, {city: "New York", country: "USA"} ], friends:[ {id: 3}, {id: 13} ], email: "mail@urk.net", phone: "+380123456789" }
    • MongoDB & Drupal 10/29 Mongo Plus 1. Flexibility 2. Scalability 3. Atomicity 4. Support for various types of data 5. Object Query Language 6. Map/Reduce
    • MongoDB & Drupal 11/29 Mongo Minuses 1. Enough young product 2. Max object size - 4 Mb 3. Max DB size - 2 GB 4. Typing 5. No transaction
    • MongoDB & Drupal 12/29 Mongo and PHP Installing from a Pecl rep OR Download the library from Githab and make Detailed documentation on PHP.net and living examples in the test folder inside the library
    • MongoDB & Drupal 13/29 Mongo and Drupal. Lets be friends
    • MongoDB & Drupal 14/29 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;
    • MongoDB & Drupal 15/29 Modules
    • MongoDB & Drupal 16/29 Update 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; }
    • MongoDB & Drupal 17/29 Find 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; }
    • MongoDB & Drupal 18/29 Description task Each user can be: - name - surname - gender - post - any number of addresses - Friends List First and last name - required Another fields - optional
    • MongoDB & Drupal 19/29 Structure - alone Users { Address "uid" : 5, { "first_name" : "Jon", "uid" : 5, "last_name" : "Smit", "address" : [ { "gender " : "male", "country" : "USA", "post" : "manager", "city" : "New York", } "address" : "st Jimmy street 4 " }, { Friends "country" : "Great Brithan", { "city" : "London", "users" : [ "address" : "Queen palace" { "uid" : 1 }, { "uid" : 7 } }] ] } }
    • MongoDB & Drupal 20/29 The structure - all-inclusive Users { "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" : [ { ... } ] }] }
    • MongoDB & Drupal 21/29 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“ } ] }
    • MongoDB & Drupal 22/29 hook_user_update 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); } }
    • MongoDB & Drupal 23/29 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
    • MongoDB & Drupal 24/29 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; }
    • MongoDB & Drupal 25/29 Extract sub-fields $top_users = dc_mongodb_aggregate( { „users‟, "uid" : 1, array( "friend_name" : “Bob", array($unwind => $friends), "frined_id" : 5 array( }, $project => array( ... uid => 1, { _id => 0, "uid" : 1, friend_uid => $friends.uid, "friend_name" : “Alex", friend_name => $friends.name, "frined_id" : 12 ) } ));
    • MongoDB & Drupal 26/29 Add virtual field { "uid" : 1, array( "friend_name" : “Bob", $project => array( "frined_id" : 5, "count " : 1 ... }, count => array( ... $add => 1 { ) "uid" : 1, ) "friend_name" : “Alex", ) "frined_id" : 12, "count" : 1 }
    • MongoDB & Drupal 27/29 Group array( { "_id " : { $group => array( "user_name " : “Bob", _id => array( "user_id " : 5 user_name => $friend_name, }, user_id => $friend_uid, "count " : 3 ), }, count => array( ... $sum => $count { ) "_id " : { ) "user_name " : “Alex", "user_id " : 2 ), }, "count " : 12 }
    • MongoDB & Drupal 28/29 Add sort and limit array( $sort => array( { count => -1 "_id " : { ) "user_name " : “Alex", ), "user_id " : 2 array( }, $limit => 1 "count " : 12 ) }
    • MongoDB & Drupal 29/29 Questions ? Contact: E-mail: pavel.gorbach@volcanoideas.com Skype: rgnrok