0
Pavel Gorbachpavel.gorbach@volcanoideas.com
MongoDB & Drupal                                2/29                      Plan        1. MySQL as standard.        2. Prob...
MongoDB & Drupal                   3/29               MySQL as standard
MongoDB & Drupal                                           4/29               MySQL as standard         1. It is a reliabl...
MongoDB & Drupal                            5/29                   MySQL is SLOW                         node_load – SLOW ...
MongoDB & Drupal                             6/29             MySQL - hard structuring                        The choice b...
MongoDB & Drupal                 7/29              Reply by MongoDB   1. Very fast insert   2. No joins => Fast select   3...
MongoDB & Drupal                     8/29                   Structure MySQL
MongoDB & Drupal                                            9/29                   Structure Mongo        {            nam...
MongoDB & Drupal                 10/29                    Mongo Plus   1. Flexibility   2. Scalability   3. Atomicity   4....
MongoDB & Drupal                   11/29                   Mongo Minuses   1. Enough young product   2. Max object size - ...
MongoDB & Drupal                                      12/29                   Mongo and PHP                          Insta...
MongoDB & Drupal                  13/29              Mongo and Drupal.               Lets be friends
MongoDB & Drupal                                          14/29                     Setting   #MongoDB   $conf[mongodb_con...
MongoDB & Drupal             15/29                   Modules
MongoDB & Drupal                                                         16/29                                 Update  fun...
MongoDB & Drupal                                                           17/29                                      Find...
MongoDB & Drupal                       18/29                   Description task      Each user can be:      - name      - ...
MongoDB & Drupal                                                           19/29                          Structure - alon...
MongoDB & Drupal                                             20/29          The structure - all-inclusive           Users ...
MongoDB & Drupal                                            21/29                    Structure - mixed             {      ...
MongoDB & Drupal                                                                 22/29                      hook_user_upda...
MongoDB & Drupal                                     23/29            Aggregation Framework       1. Means to calculate ag...
MongoDB & Drupal                                              24/29                       Aggregation wrapper    /**     *...
MongoDB & Drupal                                                    25/29                   Extract sub-fields $top_users ...
MongoDB & Drupal                                           26/29                  Add virtual field                       ...
MongoDB & Drupal                                                   27/29                                Group array(      ...
MongoDB & Drupal                                                 28/29                     Add sort and limit   array(    ...
MongoDB & Drupal                           29/29                   Questions ?  Contact:  E-mail: pavel.gorbach@volcanoide...
Upcoming SlideShare
Loading in...5
×

MongoDB & Drupal

1,485

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,485
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "MongoDB & Drupal"

  1. 1. Pavel Gorbachpavel.gorbach@volcanoideas.com
  2. 2. 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
  3. 3. MongoDB & Drupal 3/29 MySQL as standard
  4. 4. 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
  5. 5. MongoDB & Drupal 5/29 MySQL is SLOW node_load – SLOW user_load – SLOW JOIN - SLOW
  6. 6. MongoDB & Drupal 6/29 MySQL - hard structuring The choice between a set of tables or columns empty 1. Slow selection 2. Waste of memory
  7. 7. MongoDB & Drupal 7/29 Reply by MongoDB 1. Very fast insert 2. No joins => Fast select 3. Power Update
  8. 8. MongoDB & Drupal 8/29 Structure MySQL
  9. 9. 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" }
  10. 10. 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
  11. 11. 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
  12. 12. 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
  13. 13. MongoDB & Drupal 13/29 Mongo and Drupal. Lets be friends
  14. 14. 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;
  15. 15. MongoDB & Drupal 15/29 Modules
  16. 16. 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; }
  17. 17. 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; }
  18. 18. 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
  19. 19. 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 } }] ] } }
  20. 20. 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" : [ { ... } ] }] }
  21. 21. 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“ } ] }
  22. 22. 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); } }
  23. 23. 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
  24. 24. 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; }
  25. 25. 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 ) } ));
  26. 26. 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 }
  27. 27. 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 }
  28. 28. 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 ) }
  29. 29. MongoDB & Drupal 29/29 Questions ? Contact: E-mail: pavel.gorbach@volcanoideas.com Skype: rgnrok
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×