本文整理汇总了PHP中MongoDB类的典型用法代码示例。如果您正苦于以下问题:PHP MongoDB类的具体用法?PHP MongoDB怎么用?PHP MongoDB使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MongoDB类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: tearDown
public function tearDown()
{
if ($this->db instanceof \MongoDB) {
$this->db->drop();
}
parent::tearDown();
}
示例2: setUp
/**
* @access protected
*/
protected function setUp()
{
$m = new Mongo();
$db = new MongoDB($m, "phpunit");
$this->object = $db->selectCollection('c');
$this->object->drop();
}
示例3: get
/**
* Fetches the object pointed to by a reference
* @link http://php.net/manual/en/mongodbref.get.php
* @static
* @param MongoDB $db Database to use
* @param array $ref Reference to fetch
* @return array|null Returns the document to which the reference refers or null if the document does not exist (the reference is broken)
*/
public static function get($db, $ref)
{
if (!static::isRef($ref)) {
return null;
}
return $db->selectCollection($ref[static::$refKey])->findOne(['_id' => $ref[static::$idKey]]);
}
示例4: __construct
/**
* Creates new file collections
*
* @param mongodb $db - Database.
* @param string $prefix -
* @param mixed $chunks -
*/
public function __construct(MongoDB $db, $prefix = 'fs', $chunks = 'fs')
{
$this->db = $db;
$thisName = $prefix . '.files';
$this->chunksName = $prefix . '.chunks';
$this->chunks = $db->selectCollection($this->chunksName);
parent::__construct($db, $thisName);
}
示例5: __construct
public function __construct(\MongoDB $db, $collection_name, array $additional_find_fields = [])
{
if (false === is_string($collection_name)) {
throw new Exception('collection name must be a string');
}
$this->collection = $db->selectCollection($collection_name);
$this->additional_find_fields = $additional_find_fields;
}
示例6: exec
/**
* Execute a piece of javascript code
*
* @param MongoDB $db DB
* @param string $code javascript code
* @param array $params javascript function parameters
* @return array
*/
static function exec(MongoDB $db, $code, array $params = array())
{
$query = $db->execute($code, $params);
if (!$query["ok"]) {
exit("Execute failed:<font color=\"red\">" . $query["errmsg"] . "</font><br/>\n<pre>" . $code . "</pre>");
}
return $query["retval"];
}
示例7: __construct
/**
* Constructor.
*
* @param Connection $connection Connection used to create Collections
* @param \MongoDB $mongoDB MongoDB instance being wrapped
* @param EventManager $evm EventManager instance
* @param integer $numRetries Number of times to retry queries
* @param callable $loggerCallable The logger callable
*/
public function __construct(Connection $connection, $mongoDB, EventManager $evm, $numRetries, $loggerCallable)
{
if (!is_callable($loggerCallable)) {
throw new \InvalidArgumentException('$loggerCallable must be a valid callback');
}
parent::__construct($evm, $mongoDB->getDatabaseName());
$this->loggerCallable = $loggerCallable;
}
示例8: setUp
public function setUp()
{
parent::setUp();
$this->mongo = new MongoDB(new \MongoClient('mongodb://localhost:27017'), 'mongo_test', new InMemoryCache());
$this->collection = $this->mongo->selectCollection('test');
for ($i = 0; $i < 2; $i++) {
$this->collection->insert(['foo' => 1]);
}
}
示例9: __construct
/**
* @param Client $client
* @param \MongoDB|string $database
*/
public function __construct(Client $client, $database)
{
$this->client = $client;
if ($database instanceof \MongoDB) {
$this->database = $database;
$this->databaseName = $database->__toString();
} else {
$this->databaseName = $database;
}
}
示例10: __construct
public function __construct($host, $port, $name, $username, $password)
{
try {
$client = new \MongoClient("mongodb://{$host}:{$port}");
$this->db = new \MongoDB($client, $name);
$this->db->authenticate($username, $password);
} catch (\MongoException $e) {
die('Connection failed: ' . $e->getMessage());
}
}
示例11: testTest
public function testTest()
{
return;
$mongo = new MongoDB(new \MongoClient('mongodb://localhost:27017'), 'mongo_test', new Memcache($this->memcache, 1));
$collection = $mongo->selectCollection('test_memcache');
$collection->insert(['foo' => 1]);
$this->assertEquals(1, $collection->count());
sleep(1);
$collection->insert(['foo' => 1]);
$this->assertEquals(2, $collection->count());
}
示例12: connect
/**
* Connects to our database
*/
public function connect()
{
if (!extension_loaded('mongo')) {
throw new EMongoException(yii::t('yii', 'We could not find the MongoDB extension ( http://php.net/manual/en/mongo.installation.php ), please install it'));
}
try {
$this->_mongo = new MongoClient($this->connectionString, $this->connectOptions);
$dbname = $this->db;
$this->_db = $this->_mongo->{$dbname};
$this->_db->setWriteConcern($this->options['writeConcerns'], $this->options['wTimeoutMS']);
} catch (Exception $e) {
throw new EMongoException(yii::t('yii', 'We could not find the MongoDB extension ( http://php.net/manual/en/mongo.installation.php ), please install it'));
}
}
示例13: getCollection
public function getCollection($collName)
{
if (!$this->database) {
throw new Exception\LogicException("Can't select collection as there's no database set");
}
return $this->database->selectCollection($collName);
}
示例14: getChannelCollection
public static function getChannelCollection()
{
if (MongoConnector::$db == null) {
MongoConnector::connect();
}
return MongoConnector::$db->selectCollection("channels");
}
示例15: evaluate
/**
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
* @param mixed $other Value or object to evaluate.
* @return bool
*/
public function evaluate($other)
{
$query = array('_id' => $other);
$data = $this->db->selectCollection($this->collection)->findOne($query);
$this->found = $data[$this->property];
return ($this->found == $this->expected);
}