works:programmer:php:mongodb
Пример работы с MongoDB
Наваял некоторый пример как работать с MongoDB на будущее пригодиться
<?php class VtUsers { /** @var MongoDB $db */ private $db; public function __construct($mongo_url, $dbname) { $this->db = (new MongoClient($mongo_url))->__get($dbname); } public function add($username, $password, $mailaddr) { $registred = $this->get($username, false); if ( $registred ) return false; $this->db->users->insert([ 'username' => $username, 'password' => md5($password), 'mail' => $mailaddr, 'registred' => new MongoDate() ]); } public function ignorecase($string) { return new MongoRegex('/^' . preg_quote(strtolower($string), '/') . '$/i'); } public function mkdate(DateTime $datetime) { return new MongoDate($datetime->getTimestamp()); } public function get($username, $good_users=true) { $selector = [ 'username' => $this->ignorecase($username) ]; if ( $good_users ) { $selector['disabled'] = ['$ne' => 1]; $selector['inactive'] = ['$ne' => 1]; } return $this->db->users->findOne($selector); } } //$url = 'mongodb://eugene:password@localhost:27017/'; $usr = new VtUsers('mongodb://localhost:27017', 'billing_lan'); print_r( $usr->get('LikePro')); /* if ( ! $modb->users->findOne(['username'=>new MongoRegex($username)])) { $modb->users->insert([ 'username'=>'Eugene', 'password'=>'password', 'mail'=>'example@example.com', 'registred'=>new MongoDate() ]); } $cursor = $modb->users->find(['username'=>new MongoRegex($username)]); foreach ( $cursor as $document ) { print_r($document); }
works/programmer/php/mongodb.txt · Последнее изменение: 2018/12/15 17:18 — 127.0.0.1