Palestra sobre MongoDB com PHP no PHP'n'Rio

46
com Jean Carlo Nascimento aka SUISSA

description

Palestra sobre MongoDB com PHP no PHP'n'Rio

Transcript of Palestra sobre MongoDB com PHP no PHP'n'Rio

Page 1: Palestra sobre MongoDB com PHP no PHP'n'Rio

com

Jean Carlo Nascimento aka SUISSA

Page 2: Palestra sobre MongoDB com PHP no PHP'n'Rio

github.com/suissaabout.me/suissa

nosqlbr.com.br@osuissa

Page 3: Palestra sobre MongoDB com PHP no PHP'n'Rio

NOSQLO que é?

Quais são?Por que usar?

Onde usar?

Page 4: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 5: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 6: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 7: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 8: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 9: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 10: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 11: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 12: Palestra sobre MongoDB com PHP no PHP'n'Rio

key/value

Page 13: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 14: Palestra sobre MongoDB com PHP no PHP'n'Rio

graph

Page 15: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 16: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 17: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 18: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 19: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 20: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 21: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 22: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 23: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 24: Palestra sobre MongoDB com PHP no PHP'n'Rio

Terminologia

SQL RDBMS

DATABASETABLEROWSQUERYINDEXPARTITION

MongoDB    

DATABASECOLLECTIONJSON DOCUMENTQUERYINDEXSHARD

Page 25: Palestra sobre MongoDB com PHP no PHP'n'Rio

INSERIR

INSERT INTO USERS VALUES(1,1)

$db->users->insert(array("a" => 1, "b" => 1));

//ou$query = array(    'usuario'    => "suissa",    'email' => "[email protected]");

$db->collection->insert($query);

Page 26: Palestra sobre MongoDB com PHP no PHP'n'Rio

CONSULTAR

select * from tabela where nome = 'Jean Nascimento' 

$filter = array( "nome" => "Jean Nascimento" );$cursor = $collection->find($filter);foreach ($cursor as $arr) {     echo $arr["nome"]. " - " . $arr["_id"] . "<br />"; } 

Page 27: Palestra sobre MongoDB com PHP no PHP'n'Rio

CONSULTAR

select * from tabela where nome LIKE 'J___ Nascimento'

$filter = array( "nome" => new MongoRegex('/^J[a-Z]{3}Nascimento/i' );

select * from tabela where nome LIKE '%Nasc%' 

$filter = array(   'title' => new MongoRegex('/.Nasc./i') );

Page 28: Palestra sobre MongoDB com PHP no PHP'n'Rio

ALTERAR

update tarefas set Tarefa='Terminar artigo' where Usuario='suissa'

$db->collection->update(    array('Usuario'=> 'suissa'),     array('$set' => array('Tarefa' => 'Terminar artigo')));   

update users set a=a+2 where b='q'

$db->users->update(array("b" => "q"), array('$inc => array("a" => 2)));

Page 29: Palestra sobre MongoDB com PHP no PHP'n'Rio

EXCLUIR

delete from usuarios where Usuario = 'suissa'

$db->collection->remove(array('Usuario'=> 'suissa'));

Page 30: Palestra sobre MongoDB com PHP no PHP'n'Rio

EXPLAIN

EXPLAIN SELECT * FROM users WHERE z=3

$db->users->find(array("z" => 3))->explain()

Page 31: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 32: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 33: Palestra sobre MongoDB com PHP no PHP'n'Rio
Page 34: Palestra sobre MongoDB com PHP no PHP'n'Rio

 

Page 35: Palestra sobre MongoDB com PHP no PHP'n'Rio

Talk is cheap. Show me the 

code.

Page 36: Palestra sobre MongoDB com PHP no PHP'n'Rio

 $nome_banco = ‘prioridades’;$nome_collection = ‘tarefas’;$this->conexao = new Mongo();$this->db = $this->conexao->$nome_banco;$this->collection = $this->db->$nome_collection;

Page 37: Palestra sobre MongoDB com PHP no PHP'n'Rio

 

function inserir(){    $this->query = array(    ‘UsuarioID’ => $this->UsuarioID,    ‘Usuario’ => $this->Usuario,    ‘Tarefa’ => $this->Tarefa,    ‘Tipo’ => $this->Tipo,//Obrigatório, idéia, outro    ‘Prioridade’ => $this->Prioridade);    $this->collection->insert($this->query);}

Page 38: Palestra sobre MongoDB com PHP no PHP'n'Rio

 

function excluir(){    $this->mongo_id = new MongoID($this->_id);    $this->collection->remove(array(‘_id’ => $this->mongo_id));}    

Page 39: Palestra sobre MongoDB com PHP no PHP'n'Rio

 

function mudar_tarefa(){    $this->mongo_id = new MongoID($this->_id);    $this->collection->update(array(‘_id’ => $this->mongo_id), array(‘$set’ => array(‘Tarefa’ => $this->Tarefa)), false);} 

Page 40: Palestra sobre MongoDB com PHP no PHP'n'Rio

 

function mudar_tarefa(){    $this->mongo_id = new MongoID($this->_id);    $this->collection->update(array(‘_id’ => $this->mongo_id), array(‘$set’ => array(‘Tarefa’ => $this->Tarefa)), false);} 

Page 41: Palestra sobre MongoDB com PHP no PHP'n'Rio

 function mudar_prioridade(){    $this->mongo_id = new MongoID($this->_id);    if($this->modo==’up’){        $this->collection->update(            array(‘_id’ => $this->mongo_id),                             array(‘$inc’ => array(‘Prioridade’ => 1)),             false        );    } elseif($this->modo==’down’){        $this->collection->update(            array(‘_id’ => $this->mongo_id),             array(‘$inc’ => array(‘Prioridade’ => -1)),             false        );    }}

Page 42: Palestra sobre MongoDB com PHP no PHP'n'Rio

 

https://github.com/suissa/mongodb-exemplos

Page 43: Palestra sobre MongoDB com PHP no PHP'n'Rio

Frameworks

CakePHPCodeigniterDoctrineDrupalFat-FreeKohana

LithiumMemcachedSymfony 2TechMVCVorkYiiZend

Page 44: Palestra sobre MongoDB com PHP no PHP'n'Rio

Standalone Tools

ActiveMongo

MapReduce API

Mongofilesystem

Mandango

MongoDb PHP ODM

Mongodloid

MongoQueue

MongoRecord

Morph

simplemongophp

Uniform Server 6-Carbo with MongoDB and phpMoAdmin

Page 45: Palestra sobre MongoDB com PHP no PHP'n'Rio

Referências

http://www.nosqlbr.com.br/http://nosql-database.org/http://pt.wikipedia.org/wiki/NoSQLhttp://www.mongodb.org/

Page 46: Palestra sobre MongoDB com PHP no PHP'n'Rio