SlideShare a Scribd company logo
1 of 59
Download to read offline
from   MySQL

       to

               mongoDB
                                  nightsailer
                 March 3, 2011 / Mongo Beijing
About.me


       @nightsailer
     nightsailer # gmail.com
    http://nightsailer.com/
http://github.com/nightsailer
Mysql data type
      char / varchar
        Int/BigInt
  Float/Double/Decimal
        Blob/Text
            .....
BSON   [bee · sahn]




   Byte              Object Id
Int32/Int64            Array
  Double              Boolean
                  Javascript code
                 Embed document
                        .....

                                    http://bsonspec.org/
BSON

                  Object Id

   0    1 2 3     4 5 6      7 8 9 10 11
       utc time    machine    pid inc/random

  Enforce collection unique: _id
  Should global/cluster unique
BSON

         Date / Timestamp

       Perl: DateTime / Mongo::Timestamp
       PHP: MongoDate / MongoDatetime


  *             Date         Int64
BSON

            Binary Data
   Perl: string
                  my $default_avatar = "xFFxFExFF";
                  $collection->insert({"avatar_file" => $string});


   PHP:MongoBinData

            * 4Mb/8Mb(1.8+)
Create Database ....     No!
   Create Table ....      No!
      Alter Table ....         No!
Table => Collection
Row => Document
            Embed document

Embed document : Array/List/Hash
_id
           Collection

                   Array/List

Object Id => perl: MongoDB::OID   php: MongoId
Insert/Save
perl:
    $db->user->create({name=>’ns’});
  $db->user->save({_id=>5,name=>’ns’});

        SQL Injection             .
Update
>db.user.update({_id:5},{name:’ns’,‘email’:‘xxx’});




      mysql
Upsert



>db.user.update({_id:5},{_id:5,name:’ns’,email:‘xxx’},true)



                                   _id,
In-place Updates
$inc $push $pushAll $pull $pullAll $addToSet
                   $set
                    ....
                 Atomic
     modifier
       Array/Hash              $set)
Upsert & modifier
               $modifier                                           _id

  >db.art_track.update({ art_id:25,d:20110303 },
{ $addToSet:{follower:234},$inc:{views:1}},true);




 > db.art_track.find()
 { "_id" : ObjectId("4d6df20cb7fc9b3c1329c917"), "art_id" : 25, "d" : 20110303, "follower" :
 [ 234 ], "views" : 2 }
Select => Find
>db.user.find({city:‘beijing’})
          .sort({created_on:-1})
                .limit(20);
Cursor
  when find & run command
        cursor->next

OP_QEURY           OP_GETMORE


                       cursor
Order by => Sort
Sort Limit   Cursor
Joins ? No!



              ...
Query Modifier
                                    :
$ne, $in, $nin, $mod, $all, $size, $exists, $type, ..
             $lt, $lte, $gt, $gte, $ne,
                          ...
Command
                                             :
     count,map/reduce, group,...
    >db.$cmd.findOne(cmd_query_obj);


db.$cmd.findOne(‘user’) == db.user.count()
FindAndModify
db.runCommand( { findAndModify : <collection>,
              query: { filter },
                 sort : { },
                 update: {},
                  field: {},
            new/remove: true/false
                      });
FindAndModify
#perl
my $job = db_find_and_modify {
         query => { state => 0 },
         update => { '$set' => { state => 1, ts =>
time } },
         sort => { _id => 1 }
    };
#    Mysql Auto increment
<?php
$db->command(array(
    ‘findandmodify’=>‘sequences’,
    ‘query’ => array(‘_id’=>‘user_id’),
    ‘update’ => array(‘$inc’=>array(‘val’=>1)),
    ‘new’ => true,
    ‘upsert’ => true,
));
?>
                                          *update/sort,   !
List commands
> db.runCommand({listCommands:1})


        Quick reference card
 http://www.10gen.com/reference
Index
  db.foo.ensureIndex({a:1})
db.foo.ensureIndex({a:1,b:-1})

    collection        64    index
     query           1     index
Multikeys
              Array/Object

               >db.art.ensureIndex({tags:1});
>db.art.find({tags:’    ’}).sort({created_on:-1}).limit(10);

         >db.user.ensureIndex({‘profile.age’:1});
         >db.user.find({"profile.age":{$gte:18}});
Special index / 1.8+
Sparse Indexes
   >db.user.ensureIndex({sina_account:1},{sparse:true});
   >db.user.findOne({sina_account:‘xxx’});

Covered Indexes
   >db.user.ensureIndex({_id:1,passport:1,state:1});
   >db.user.findOne({passport:‘xxx’},{_id:0,passport:
   1,state:1});
Like %mongoDB% ?

 > db.count({title:/mongodb/i});



   Regex => index
collection
     db.fulltext.save({
index_name: [ word1,word2]
   {                    }
            ...
             })
index_name
db.fullext.find({index_name:{$all:
          [w1,w2,w3]}})
      .sort({updated_on:-1})
Sum/Group by => ?
Group command          +         db
  Map/Reduce: good,   sharding
Alternate approach
Live: counter field =>$inc
     Backend service:
                  => Gearman: workers
ORM,easy.
PHP: Zend/Symfony/CI ...
               Ruby:Mongoid

           Perl: Mongoose MongoDBx
<?php
//        model
class Lgk_Model_Art extends Lgk_Core_Model_Base {
    protected $collection = "art";
    protected $schema = array(
         'category_id' => 0,
         'tags' => array(),
         'fav_tags' => array(),
         ....
    );
    protected $required_fields = array('user_id','name');
    protected $int_fields = array('user_id','category_id','deleted','published','approved','private');
    protected $created_timestamp_fields = array('created_on','updated_on');
    protected $joins = array(
         'user' =>    array('user_id' => 'Lgk_Core_Model_User'),
         'assets' => array('assets' => 'Lgk_Core_Model_Asset'),
         'category' => array('category_id' => 'Lgk_Core_Model_Category'),
         'thumbnail_asset' => array('thumb_asset_id' => 'Lgk_Core_Model_Asset'),
	     );
    protected function extra_extend_model_row(&$row) {
    }
?>
//
public function load_joins($row) {
       ...
         foreach ($this->joins as $attribute => $definition) {
             list($pk_name,$model_class) = each($definition);
             if (isset($row[$pk_name])) {
                 if (is_array($row[$pk_name])) {
                      $row[$attribute] = &DoggyX_Model_Mapper::load_model_list($row[$pk_name],$model_class);
                 }
                 else {
                      $row[$attribute] = &DoggyX_Model_Mapper::load_model($row[$pk_name],$model_class);
                 }
             }
         }
         return $row;
    }
}
GridFS
 Just specification.
Not best but good.
Write-once, read many
     db.assets:{                  db.fs.file: {
    _id:ObjectId,              _id: ObjectId,
  file_id: ObjectId,           r: (ref counter)
thumb_id:ObjectId,             hash:‘xxxxx’
          ...                          }
           }


        db.fs.files     1
                                       r
                       files         file_id
GridFS

Nginx module ?
Plack/Twiggy    AnyEvent


         Nginx                    Todo                Node.js


Varnish/Squid    proxy store

                                    Plack/Starman       /



                               ETag: file _id
Replication
           local.system.replset
      local.oplog.rs => oplog/capped



* local.usr.xxx =>                     collection


                     collection
mySQL: mmm



          ReplicaSet
      1 Primary + 2 Secondary + 1
                Arbiter


                      * Master/Slave
“SlaveOk”
<?php
$con =new Mongo(‘mongodb://s1:27017’,array
(‘replicaSet’=>true);
$con->setSlaveOkay(true);
?>
#perl
my $con = MongoDB::Connection->new(host=>‘mongodb://
localhost’,w =>2,find_master => 1);
$MongoDB::Cursor::slave_okay = 1;
my $cursor = $con->user->find;
$cursor->slave_okay = 1;


                             Driver
Auto Shard
:             :    db

                          : mongos
    shard_key:        ,
             counting
WTF?
VPN

    => VPN

&     =>

     oplog   /replay,
db.repairDatabase,

:   primary,                 !
               oplog
RAID10
XFS filesystem
kill -9 or     = Crash!



                          Durability?
                          --syncdelay     ? (60=>5)
                                    ‘w’
             db.runCommand( { getlasterror : 1 , w : 2 } )


             ReplicaSet       2   secondary
                                          -dur upgrade to 1.8!
Count is slow!

    counter
Type: string vs int

           “2” != 2

<php? $user_id = (int) $user_id; ?>
OOM Killer

> db.serverStatus()


 Swap             > Memory
Ordered hash

Perl: IxHash
               ({a=>1, b=>-1}) => IxHash->new


Ruby (<1.9): BSON::OrderedHash
PHP: Array is OK
Cursor
         last_row_id = ObjectId(‘....’);
db.activity_stream->find({_id:{$lt: last_row_id },
    user_id:20 } ).sort( {_id:-1} ).limit(10);
PHP/5.2.x: front-end
Perl 5.2.10+ / Plack / Gearman:
Daemon, large-file upload, data
process, job queue service ...

           MongoDB        :
             session store
             business data
             GridFS: Media files (user uploaded
             files, thumbnails ..)
use MongoX (host=>‘mongodb://127.0.0.1’,db=>‘test’);
use_collection 'task_queue';
db_update { state => 1, queue => 'sina_tweet' }, {
        '$set' => { state => 0 },
        '$inc' => { tries => 1 },
        },{ multiple => 1 };
db_remove { state => 1, queue => 'sina_tweet', 'tries' =>
{ '$gt' => 3 } };
db_inc {}

//
my $cnt = 0;
for_connections {
   for_dbs {
        for_collections {
          $cnt += db_count;
        } ‘art’
     } ‘db1’,‘db2’
} ‘c_1’ ,‘c_2’,‘c_arch1’,‘c_arch2’;



     http://github.com/nightsailer/mongo-x/
:
http://czone.chinavisual.com/
2009-6 rebuild on mongodb
          < 1.0
http://cvurl.cn/
     :
http://tu.chinavisual.com/
mongoDB:
                 :             GridFS: hight resolution
http://down.chinavisual.com/
                               images,photos/medium
                               size files(60mb~500mb)
http://idea.chinavisual.com/
PHP 开发      师
        job-php@chinavisual.com
 Modern Perl 开发      师
         job-perl@chinavisual.com

   mongoDB / Git / Catalyst /
Ubuntu / Xiapian / Nginx / Plack /
Moose / Node.js / Titanium / Redis

More Related Content

What's hot

Models and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and HobgoblinsModels and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and HobgoblinsRoss Tuck
 
Drush. Secrets come out.
Drush. Secrets come out.Drush. Secrets come out.
Drush. Secrets come out.Alex S
 
PHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php frameworkPHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php frameworkG Woo
 
PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)Nikita Popov
 
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)MongoSF
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data ObjectsWez Furlong
 
Database Design Patterns
Database Design PatternsDatabase Design Patterns
Database Design PatternsHugo Hamon
 
CouchDB on Android
CouchDB on AndroidCouchDB on Android
CouchDB on AndroidSven Haiges
 
Building fast interpreters in Rust
Building fast interpreters in RustBuilding fast interpreters in Rust
Building fast interpreters in RustIngvar Stepanyan
 
20110514 mongo dbチューニング
20110514 mongo dbチューニング20110514 mongo dbチューニング
20110514 mongo dbチューニングYuichi Matsuo
 
Desarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móvilesDesarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móvilesLuis Curo Salvatierra
 
Backbone.js: Run your Application Inside The Browser
Backbone.js: Run your Application Inside The BrowserBackbone.js: Run your Application Inside The Browser
Backbone.js: Run your Application Inside The BrowserHoward Lewis Ship
 
PHP 7 – What changed internally?
PHP 7 – What changed internally?PHP 7 – What changed internally?
PHP 7 – What changed internally?Nikita Popov
 
Doctrine MongoDB Object Document Mapper
Doctrine MongoDB Object Document MapperDoctrine MongoDB Object Document Mapper
Doctrine MongoDB Object Document MapperJonathan Wage
 
はじめてのMongoDB
はじめてのMongoDBはじめてのMongoDB
はじめてのMongoDBTakahiro Inoue
 
Doctrine 2
Doctrine 2Doctrine 2
Doctrine 2zfconfua
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the TrenchesJonathan Wage
 
Crazy things done on PHP
Crazy things done on PHPCrazy things done on PHP
Crazy things done on PHPTaras Kalapun
 

What's hot (20)

Models and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and HobgoblinsModels and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and Hobgoblins
 
Drush. Secrets come out.
Drush. Secrets come out.Drush. Secrets come out.
Drush. Secrets come out.
 
PHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php frameworkPHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php framework
 
PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)
 
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
 
Database Design Patterns
Database Design PatternsDatabase Design Patterns
Database Design Patterns
 
CouchDB on Android
CouchDB on AndroidCouchDB on Android
CouchDB on Android
 
Agile database access with CakePHP 3
Agile database access with CakePHP 3Agile database access with CakePHP 3
Agile database access with CakePHP 3
 
Building fast interpreters in Rust
Building fast interpreters in RustBuilding fast interpreters in Rust
Building fast interpreters in Rust
 
20110514 mongo dbチューニング
20110514 mongo dbチューニング20110514 mongo dbチューニング
20110514 mongo dbチューニング
 
Desarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móvilesDesarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móviles
 
Backbone.js: Run your Application Inside The Browser
Backbone.js: Run your Application Inside The BrowserBackbone.js: Run your Application Inside The Browser
Backbone.js: Run your Application Inside The Browser
 
PHP 7 – What changed internally?
PHP 7 – What changed internally?PHP 7 – What changed internally?
PHP 7 – What changed internally?
 
Php 101: PDO
Php 101: PDOPhp 101: PDO
Php 101: PDO
 
Doctrine MongoDB Object Document Mapper
Doctrine MongoDB Object Document MapperDoctrine MongoDB Object Document Mapper
Doctrine MongoDB Object Document Mapper
 
はじめてのMongoDB
はじめてのMongoDBはじめてのMongoDB
はじめてのMongoDB
 
Doctrine 2
Doctrine 2Doctrine 2
Doctrine 2
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the Trenches
 
Crazy things done on PHP
Crazy things done on PHPCrazy things done on PHP
Crazy things done on PHP
 

Viewers also liked

视觉中国的MongoDB应用实践(QConBeijing2011)
视觉中国的MongoDB应用实践(QConBeijing2011)视觉中国的MongoDB应用实践(QConBeijing2011)
视觉中国的MongoDB应用实践(QConBeijing2011)Night Sailer
 
下吧开发总结
下吧开发总结下吧开发总结
下吧开发总结Night Sailer
 
My Daughter Power Point
My Daughter Power PointMy Daughter Power Point
My Daughter Power PointTalena McGill
 
IPython vers Jupyter
IPython vers JupyterIPython vers Jupyter
IPython vers JupyterMike Bright
 
8 minute MongoDB tutorial slide
8 minute MongoDB tutorial slide8 minute MongoDB tutorial slide
8 minute MongoDB tutorial slideiammutex
 

Viewers also liked (6)

视觉中国的MongoDB应用实践(QConBeijing2011)
视觉中国的MongoDB应用实践(QConBeijing2011)视觉中国的MongoDB应用实践(QConBeijing2011)
视觉中国的MongoDB应用实践(QConBeijing2011)
 
Sts 131 landing
Sts 131 landingSts 131 landing
Sts 131 landing
 
下吧开发总结
下吧开发总结下吧开发总结
下吧开发总结
 
My Daughter Power Point
My Daughter Power PointMy Daughter Power Point
My Daughter Power Point
 
IPython vers Jupyter
IPython vers JupyterIPython vers Jupyter
IPython vers Jupyter
 
8 minute MongoDB tutorial slide
8 minute MongoDB tutorial slide8 minute MongoDB tutorial slide
8 minute MongoDB tutorial slide
 

Similar to From mysql to MongoDB(MongoDB2011北京交流会)

Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128PrinceGuru MS
 
Zend Framework Study@Tokyo #2
Zend Framework Study@Tokyo #2Zend Framework Study@Tokyo #2
Zend Framework Study@Tokyo #2Shinya Ohyanagi
 
mongodb-introduction
mongodb-introductionmongodb-introduction
mongodb-introductionTse-Ching Ho
 
PHP Static Code Review
PHP Static Code ReviewPHP Static Code Review
PHP Static Code ReviewDamien Seguy
 
San Francisco Java User Group
San Francisco Java User GroupSan Francisco Java User Group
San Francisco Java User Groupkchodorow
 
Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Jeff Carouth
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7chuvainc
 
Redis the better NoSQL
Redis the better NoSQLRedis the better NoSQL
Redis the better NoSQLOpenFest team
 
Python magicmethods
Python magicmethodsPython magicmethods
Python magicmethodsdreampuf
 
Drupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary EditionDrupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary Editionddiers
 
Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQLddiers
 
Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksNate Abele
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency InjectionRifat Nabi
 
Open Source Search: An Analysis
Open Source Search: An AnalysisOpen Source Search: An Analysis
Open Source Search: An AnalysisJustin Finkelstein
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsPierre MARTIN
 

Similar to From mysql to MongoDB(MongoDB2011北京交流会) (20)

DataMapper
DataMapperDataMapper
DataMapper
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128
 
Zend Framework Study@Tokyo #2
Zend Framework Study@Tokyo #2Zend Framework Study@Tokyo #2
Zend Framework Study@Tokyo #2
 
Mongodb workshop
Mongodb workshopMongodb workshop
Mongodb workshop
 
mongodb-introduction
mongodb-introductionmongodb-introduction
mongodb-introduction
 
PHP Static Code Review
PHP Static Code ReviewPHP Static Code Review
PHP Static Code Review
 
San Francisco Java User Group
San Francisco Java User GroupSan Francisco Java User Group
San Francisco Java User Group
 
Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4
 
Quebec pdo
Quebec pdoQuebec pdo
Quebec pdo
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7
 
Redis the better NoSQL
Redis the better NoSQLRedis the better NoSQL
Redis the better NoSQL
 
Python magicmethods
Python magicmethodsPython magicmethods
Python magicmethods
 
Spl Not A Bridge Too Far phpNW09
Spl Not A Bridge Too Far phpNW09Spl Not A Bridge Too Far phpNW09
Spl Not A Bridge Too Far phpNW09
 
Drupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary EditionDrupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary Edition
 
Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQL
 
MongoDB (Advanced)
MongoDB (Advanced)MongoDB (Advanced)
MongoDB (Advanced)
 
Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate Frameworks
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Open Source Search: An Analysis
Open Source Search: An AnalysisOpen Source Search: An Analysis
Open Source Search: An Analysis
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP Applications
 

Recently uploaded

Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 

From mysql to MongoDB(MongoDB2011北京交流会)

  • 1. from MySQL to mongoDB nightsailer March 3, 2011 / Mongo Beijing
  • 2. About.me @nightsailer nightsailer # gmail.com http://nightsailer.com/ http://github.com/nightsailer
  • 3. Mysql data type char / varchar Int/BigInt Float/Double/Decimal Blob/Text .....
  • 4. BSON [bee · sahn] Byte Object Id Int32/Int64 Array Double Boolean Javascript code Embed document ..... http://bsonspec.org/
  • 5. BSON Object Id 0 1 2 3 4 5 6 7 8 9 10 11 utc time machine pid inc/random Enforce collection unique: _id Should global/cluster unique
  • 6. BSON Date / Timestamp Perl: DateTime / Mongo::Timestamp PHP: MongoDate / MongoDatetime * Date Int64
  • 7. BSON Binary Data Perl: string my $default_avatar = "xFFxFExFF"; $collection->insert({"avatar_file" => $string}); PHP:MongoBinData * 4Mb/8Mb(1.8+)
  • 8. Create Database .... No! Create Table .... No! Alter Table .... No!
  • 9. Table => Collection Row => Document Embed document Embed document : Array/List/Hash
  • 10. _id Collection Array/List Object Id => perl: MongoDB::OID php: MongoId
  • 11. Insert/Save perl: $db->user->create({name=>’ns’}); $db->user->save({_id=>5,name=>’ns’}); SQL Injection .
  • 14. In-place Updates $inc $push $pushAll $pull $pullAll $addToSet $set .... Atomic modifier Array/Hash $set)
  • 15. Upsert & modifier $modifier _id >db.art_track.update({ art_id:25,d:20110303 }, { $addToSet:{follower:234},$inc:{views:1}},true); > db.art_track.find() { "_id" : ObjectId("4d6df20cb7fc9b3c1329c917"), "art_id" : 25, "d" : 20110303, "follower" : [ 234 ], "views" : 2 }
  • 16. Select => Find >db.user.find({city:‘beijing’}) .sort({created_on:-1}) .limit(20);
  • 17. Cursor when find & run command cursor->next OP_QEURY OP_GETMORE cursor
  • 18. Order by => Sort Sort Limit Cursor
  • 19. Joins ? No! ...
  • 20. Query Modifier : $ne, $in, $nin, $mod, $all, $size, $exists, $type, .. $lt, $lte, $gt, $gte, $ne, ...
  • 21. Command : count,map/reduce, group,... >db.$cmd.findOne(cmd_query_obj); db.$cmd.findOne(‘user’) == db.user.count()
  • 22. FindAndModify db.runCommand( { findAndModify : <collection>, query: { filter }, sort : { }, update: {}, field: {}, new/remove: true/false });
  • 23. FindAndModify #perl my $job = db_find_and_modify { query => { state => 0 }, update => { '$set' => { state => 1, ts => time } }, sort => { _id => 1 } }; # Mysql Auto increment <?php $db->command(array( ‘findandmodify’=>‘sequences’, ‘query’ => array(‘_id’=>‘user_id’), ‘update’ => array(‘$inc’=>array(‘val’=>1)), ‘new’ => true, ‘upsert’ => true, )); ?> *update/sort, !
  • 24. List commands > db.runCommand({listCommands:1}) Quick reference card http://www.10gen.com/reference
  • 26. Multikeys Array/Object >db.art.ensureIndex({tags:1}); >db.art.find({tags:’ ’}).sort({created_on:-1}).limit(10); >db.user.ensureIndex({‘profile.age’:1}); >db.user.find({"profile.age":{$gte:18}});
  • 27. Special index / 1.8+ Sparse Indexes >db.user.ensureIndex({sina_account:1},{sparse:true}); >db.user.findOne({sina_account:‘xxx’}); Covered Indexes >db.user.ensureIndex({_id:1,passport:1,state:1}); >db.user.findOne({passport:‘xxx’},{_id:0,passport: 1,state:1});
  • 28. Like %mongoDB% ? > db.count({title:/mongodb/i}); Regex => index
  • 29. collection db.fulltext.save({ index_name: [ word1,word2] { } ... })
  • 30. index_name db.fullext.find({index_name:{$all: [w1,w2,w3]}}) .sort({updated_on:-1})
  • 31. Sum/Group by => ? Group command + db Map/Reduce: good, sharding
  • 32. Alternate approach Live: counter field =>$inc Backend service: => Gearman: workers
  • 33. ORM,easy. PHP: Zend/Symfony/CI ... Ruby:Mongoid Perl: Mongoose MongoDBx
  • 34. <?php // model class Lgk_Model_Art extends Lgk_Core_Model_Base { protected $collection = "art"; protected $schema = array( 'category_id' => 0, 'tags' => array(), 'fav_tags' => array(), .... ); protected $required_fields = array('user_id','name'); protected $int_fields = array('user_id','category_id','deleted','published','approved','private'); protected $created_timestamp_fields = array('created_on','updated_on'); protected $joins = array( 'user' => array('user_id' => 'Lgk_Core_Model_User'), 'assets' => array('assets' => 'Lgk_Core_Model_Asset'), 'category' => array('category_id' => 'Lgk_Core_Model_Category'), 'thumbnail_asset' => array('thumb_asset_id' => 'Lgk_Core_Model_Asset'), ); protected function extra_extend_model_row(&$row) { } ?> // public function load_joins($row) { ... foreach ($this->joins as $attribute => $definition) { list($pk_name,$model_class) = each($definition); if (isset($row[$pk_name])) { if (is_array($row[$pk_name])) { $row[$attribute] = &DoggyX_Model_Mapper::load_model_list($row[$pk_name],$model_class); } else { $row[$attribute] = &DoggyX_Model_Mapper::load_model($row[$pk_name],$model_class); } } } return $row; } }
  • 36. Write-once, read many db.assets:{ db.fs.file: { _id:ObjectId, _id: ObjectId, file_id: ObjectId, r: (ref counter) thumb_id:ObjectId, hash:‘xxxxx’ ... } } db.fs.files 1 r files file_id
  • 38. Plack/Twiggy AnyEvent Nginx Todo Node.js Varnish/Squid proxy store Plack/Starman / ETag: file _id
  • 39. Replication local.system.replset local.oplog.rs => oplog/capped * local.usr.xxx => collection collection
  • 40. mySQL: mmm ReplicaSet 1 Primary + 2 Secondary + 1 Arbiter * Master/Slave
  • 41. “SlaveOk” <?php $con =new Mongo(‘mongodb://s1:27017’,array (‘replicaSet’=>true); $con->setSlaveOkay(true); ?> #perl my $con = MongoDB::Connection->new(host=>‘mongodb:// localhost’,w =>2,find_master => 1); $MongoDB::Cursor::slave_okay = 1; my $cursor = $con->user->find; $cursor->slave_okay = 1; Driver
  • 42. Auto Shard : : db : mongos shard_key: , counting
  • 43. WTF?
  • 44. VPN => VPN & => oplog /replay,
  • 45. db.repairDatabase, : primary, ! oplog
  • 47. kill -9 or = Crash! Durability? --syncdelay ? (60=>5) ‘w’ db.runCommand( { getlasterror : 1 , w : 2 } ) ReplicaSet 2 secondary -dur upgrade to 1.8!
  • 48. Count is slow! counter
  • 49. Type: string vs int “2” != 2 <php? $user_id = (int) $user_id; ?>
  • 51. Ordered hash Perl: IxHash ({a=>1, b=>-1}) => IxHash->new Ruby (<1.9): BSON::OrderedHash PHP: Array is OK
  • 52. Cursor last_row_id = ObjectId(‘....’); db.activity_stream->find({_id:{$lt: last_row_id }, user_id:20 } ).sort( {_id:-1} ).limit(10);
  • 53. PHP/5.2.x: front-end Perl 5.2.10+ / Plack / Gearman: Daemon, large-file upload, data process, job queue service ... MongoDB : session store business data GridFS: Media files (user uploaded files, thumbnails ..)
  • 54. use MongoX (host=>‘mongodb://127.0.0.1’,db=>‘test’); use_collection 'task_queue'; db_update { state => 1, queue => 'sina_tweet' }, { '$set' => { state => 0 }, '$inc' => { tries => 1 }, },{ multiple => 1 }; db_remove { state => 1, queue => 'sina_tweet', 'tries' => { '$gt' => 3 } }; db_inc {} // my $cnt = 0; for_connections { for_dbs { for_collections { $cnt += db_count; } ‘art’ } ‘db1’,‘db2’ } ‘c_1’ ,‘c_2’,‘c_arch1’,‘c_arch2’; http://github.com/nightsailer/mongo-x/
  • 56. http://cvurl.cn/ : http://tu.chinavisual.com/
  • 57. mongoDB: : GridFS: hight resolution http://down.chinavisual.com/ images,photos/medium size files(60mb~500mb)
  • 59. PHP 开发 师 job-php@chinavisual.com Modern Perl 开发 师 job-perl@chinavisual.com mongoDB / Git / Catalyst / Ubuntu / Xiapian / Nginx / Plack / Moose / Node.js / Titanium / Redis