本文整理汇总了PHP中Mongo类的典型用法代码示例。如果您正苦于以下问题:PHP Mongo类的具体用法?PHP Mongo怎么用?PHP Mongo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Mongo类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testBasic
public function testBasic()
{
$r1 = new MongoRegex("//");
$this->assertEquals($r1->regex, "");
$this->assertEquals($r1->flags, "");
$r2 = new MongoRegex("/foo/bar");
$this->assertEquals($r2->regex, "foo");
$this->assertEquals($r2->flags, "bar");
$r3 = new MongoRegex($r2);
$this->assertEquals($r3->regex, "foo");
$this->assertEquals($r3->flags, "bar");
$stupid_str = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
$rstupid = new MongoRegex("/{$stupid_str}/flagflagflagflagflag");
$this->assertEquals($rstupid->regex, $stupid_str);
$this->assertEquals($rstupid->flags, "flagflagflagflagflag");
$m = new Mongo();
$c = $m->selectCollection('phpunit', 'regex');
$c->drop();
$c->insert(array('x' => 0, 'r1' => $r1));
$c->insert(array('x' => 1, 'r2' => $r2));
$c->insert(array('x' => 2, 'stupid' => $rstupid));
$obj = $c->findOne(array('x' => 0));
$this->assertEquals($obj['r1']->regex, "");
$this->assertEquals($obj['r1']->flags, "");
$obj = $c->findOne(array('x' => 1));
$this->assertEquals($obj['r2']->regex, "foo");
$this->assertEquals($obj['r2']->flags, "bar");
$obj = $c->findOne(array('x' => 2));
$this->assertEquals($obj['stupid']->regex, $stupid_str);
$this->assertEquals($obj['stupid']->flags, "flagflagflagflagflag");
}
示例2: __construct
function __construct($db = null, $host = null)
{
$mongo = new Mongo($host);
$database = $mongo->selectDB($db);
$this->mongo = $mongo;
$this->database = $database;
}
示例3: __construct
private function __construct()
{
$con = new Mongo("mongodb://" . MONGO_HOST . ":27017");
$db = $con->selectDB(MONGO_DATABASE);
// Connect to Database
$this->mGrid = $db->getGridFS();
}
示例4: _UpdateReport
private static function _UpdateReport($url, $reportName)
{
$_host = parse_url($url, PHP_URL_HOST);
$_today = new MongoDate(strtotime('today'));
$connected = false;
while (!$connected) {
try {
$_mdb = new Mongo();
$connected = true;
} catch (MongoConnectionException $e) {
//echo "\nMongoConnectionException: ".$e->getMessage();
sleep(1);
}
}
$_reports = $_mdb->selectDB("googleproxy")->reports;
$_rowFilter = array('host' => $_host, 'date' => $_today);
$success = false;
while (!$success) {
try {
if (!$_reports->findOne($_rowFilter)) {
$_reports->insert(array_merge($_rowFilter, array(ReportType::CacheHitCount => array('count' => 0), ReportType::RequestCount => array('count' => 0))));
}
$success = true;
} catch (MongoCursorException $e) {
//echo "\nMongoCursorException: ".$e->getMessage();
sleep(1);
}
}
$_reports->ensureIndex(array('report' => 1, 'date' => 1), array('background' => true));
$_update = array('$inc' => array($reportName . '.count' => 1));
$_options['multiple'] = false;
$_reports->update($_rowFilter, $_update, $_options);
}
示例5: __construct
public function __construct()
{
$mongo = new Mongo();
$mongoDb = $mongo->selectDB('ContactsManager');
$this->contactsManagerCollection = new MongoCollection($mongoDb, 'Contact');
$this->contactsManagerCollection->ensureIndex(array('email' => 1), array('unique' => true, 'dropDups' => true));
}
示例6: createInstance
/**
* Creates a mongo connection and connects.
*
* @throws MongoConnectionException, Exception
* @return MongoDB database object
*/
private function createInstance()
{
//Pull these from a config file
jimport('sml.smlconfig');
$conf = new SMLConfig();
$serverList = $conf->mongo_hosts;
$database = $conf->mongo_db;
$persistent = $conf->mongo_persistent;
$paired = $conf->mongo_paired;
//End config entries
if (count($serverList) > 2) {
throw new Exception("Connection can be established to 2 or fewer instances only.");
}
if (count($serverList) == 2) {
$paired = true;
$servers = implode(',', $serverList);
} else {
$servers = $serverList[0];
}
try {
$m = new Mongo($servers, true, $persistent, $paired);
$db = $m->selectDB($database);
} catch (Exception $e) {
//we should swallow this, and likely put a site down message up..
die('<pre>' . $e->getTraceAsString());
}
return $db;
}
示例7: conn
protected function conn()
{
if (!isset(self::$_db_handle[$this->db_config_name])) {
if (false == ($iconfig = C($this->db_config_name, 'db'))) {
show_error('数据库配制文件db.config.php中 ' . $this->db_config_name . ' 未设置。');
}
$DSN = 'mongodb://';
// exp: mongodb://192.168.1.222
if ($iconfig['user']) {
$DSN .= $iconfig['user'];
}
if ($iconfig['password']) {
$DSN .= ':' . $iconfig['password'];
}
if ($DSN != 'mongodb://') {
$DSN .= '@';
}
if ($iconfig['host']) {
$DSN .= $iconfig['host'];
}
if (!empty($iconfig['port'])) {
$DSN .= ':' . $iconfig['port'];
}
$mongoDB = new Mongo($DSN);
self::$_db_handle[$this->db_config_name] = $this->db = $mongoDB->selectDB($iconfig['dbname']);
} else {
$this->db = self::$_db_handle[$this->db_config_name];
}
return $this->db;
}
示例8: __construct
/**
* $dsn has to contain db_name after the host. E.g. "mongodb://localhost:27017/mongo_test_db"
*
* @static
* @param $dsn
* @param $user
* @param $password
* @return \Mongo
* @throws \Exception
*/
public function __construct($dsn, $user, $password)
{
/* defining DB name */
$this->dbName = substr($dsn, strrpos($dsn, '/') + 1);
if (strlen($this->dbName) == 0) {
throw new \Exception('Please specify valid $dsn with DB name after the host:port');
}
/* defining host */
if (false !== strpos($dsn, 'mongodb://')) {
$this->host = str_replace('mongodb://', '', $dsn);
} else {
$this->host = $dsn;
}
$this->host = rtrim(str_replace($this->dbName, '', $this->host), '/');
$options = array('connect' => TRUE);
if ($user && $password) {
$options += array('username' => $user, 'password' => $password);
}
try {
$m = new \Mongo($dsn, $options);
$this->dbh = $m->selectDB($this->dbName);
} catch (\MongoConnectionException $e) {
throw new \Exception(sprintf('Failed to open Mongo connection: %s', $e->getMessage()));
}
$this->dsn = $dsn;
$this->user = $user;
$this->password = $password;
}
示例9: __construct
function __construct()
{
$this->conf = new Conf();
$m = new Mongo();
$db = $m->selectDB($this->conf->db());
$this->selfColl = $db->zone;
}
示例10: __construct
public function __construct()
{
$debugOutput = false;
$mongoDir = "/usr";
if (!file_exists($mongoDir . '/bin/mongod')) {
throw new PHPUnit_Framework_SkippedTestError('mongo daemon not found');
}
static $m;
if (!isset($m)) {
$port = Kwf_Util_Tcp::getFreePort(rand(27020, 30000));
$cmd = "php " . KWF_PATH . "/bootstrap.php test forward --controller=kwf_model_mongo_run-temp-mongo --port={$port}";
if ($debugOutput) {
echo $cmd . "\n";
$descriptorspec = array(1 => STDOUT, 2 => STDOUT);
} else {
$cmd .= " 2>&1 >/dev/null";
$descriptorspec = array(1 => array('pipe', 'w'), 2 => STDOUT);
}
self::$_proc = new Kwf_Util_Proc($cmd, $descriptorspec);
sleep(3);
$m = new Mongo("mongodb://localhost:{$port}");
register_shutdown_function(array('Kwf_Model_Mongo_TestModel', 'shutDown'));
}
$db = $m->selectDB("modeltest");
$db->selectCollection($this->_collection)->drop();
$config = array('db' => $db);
parent::__construct($config);
}
示例11: createConnection
protected static function createConnection()
{
$server = isset($GLOBALS['mongo_server']) ? $GLOBALS['mongo_server'] : 'mongodb://localhost:27017';
$dbName = isset($GLOBALS['mongo_db_name']) ? $GLOBALS['mongo_db_name'] : 'task_queue_tests';
$mongo = new \Mongo($server);
return $mongo->selectDb($dbName);
}
示例12: registerServices
/**
* Registers the module-only services
*
* @param Phalcon\DI $di
*/
public function registerServices($di)
{
/**
* Read configuration
*/
$config = (include __DIR__ . "/config/config.php");
$di['view']->setViewsDir(__DIR__ . '/views/');
/**
* The database connection is created based on the parameters defined in the configuration file
*/
$di['db'] = function () use($config) {
return new DbAdapter(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname));
};
/**
* The mongo connection is created
*/
$di['mongo'] = function () {
$mongo = new \Mongo();
return $mongo->selectDb("biko");
};
/**
* ODM Collection Manager
*/
$di['collectionManager'] = function () {
return new CollectionManager();
};
/**
* Unique Cart ID generator
*/
$di['unique'] = function () {
return new Generator();
};
}
示例13: add_point
public static function add_point($lat, $lon, $point_name)
{
$m = new Mongo();
$db = $m->selectDB("your_db");
$collection = $db->your_collection;
$collection->insert(array("point_name" => $point_name, "loc" => array("lon" => $lon, "lat" => $lat)));
}
示例14: __construct
/**
* 构造查询
*
* @param Mongo $mongo MongoDB连接
* @param string $db 数据库
* @param string $collection 集合
*/
function __construct(Mongo $mongo, $db, $collection)
{
$this->_dbName = $db;
$this->_collectionName = $collection;
$this->_db = $mongo->selectDB($this->_dbName);
$this->_collection = $mongo->selectCollection($this->_dbName, $this->_collectionName);
}
示例15: connect
public function connect()
{
// Connect to the database and return the collection
$mongo = new Mongo('mongodb://localhost');
$db = $mongo->selectDB('test');
return $db->selectCollection('locations');
}