php mongodb extension
To install the mongodb extension, do:
sudo apt-get install php-mongodb
dnf install php-mongodb
<?php
use MongoDB\Driver\Manager;
try {
$manager = new Manager("mongodb://localhost:27017/mymongodatabase");
} catch (MongoDB\Driver\Exception\Exception $e) {
echo "Exception:", $e->getMessage(), "\n";
echo "On line:", $e->getLine(), "\n";
}
$query = new MongoDB\Driver\Query([]);
$rows = $manager->executeQuery("mymongodatabase.users", $query);
foreach ($rows as $row) {
echo $row->id;
echo $row->firstname;
echo $row->lastname;
echo $row->address->street;
echo $row->address->city;
echo $row->address->country;
}
$id = 1;
$firstname = "John";
$lastname = "Doe";
$street = "Best Avenue";
$city = "Los Angeles";
$country = "USA";
try {
$bulk = new MongoDB\Driver\BulkWrite();
$doc = ['id' => $id, 'firstname' => $firstname, 'lastname' => $lastname,
'address' => ['street' => $street, 'city' => $city,
'country' => $country]];
$bulk->insert($doc);
$result = $manager->executeBulkWrite('mymongodatabase.users', $bulk);
} catch (MongoDB\Driver\Exception\Exception $e) {
echo "Exception:", $e->getMessage(), "\n";
echo "On line:", $e->getLine(), "\n";
}
?>
Connect to MongoDB
Create a MongoDB connection, that later you can query:
$manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
In the next example, you will learn how to query the connection object.
This extension close the connection automatically, it's not necessary to close manually.
Delete a document
Example for deleting all documents where name is equal to "Peter":
$bulk = new \MongoDB\Driver\BulkWrite;
$filter = ['name' => 'Peter'];
$bulk->delete($filter);
$result = $manager->executeBulkWrite('database_name.collection_name', $bulk);
Get multiple documents - find()
Example for searching multiple users with the name "Mike":
$filter = ['name' => 'Mike'];
$query = new \MongoDB\Driver\Query($filter);
$cursor = $manager->executeQuery('database_name.collection_name', $query);
foreach ($cursor as $doc) {
var_dump($doc);
}
Get one document - findOne()
Example for searching just one user with a specific id, you should do:
$options = ['limit' => 1];
$filter = ['_id' => new \MongoDB\BSON\ObjectID('578ff7c3648c940e008b457a')];
$query = new \MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery('database_name.collection_name', $query);
$cursorArray = $cursor->toArray();
if(isset($cursorArray[0])) {
var_dump($cursorArray[0]);
}
Insert document
Example for adding a document:
$document = [
'name' => 'John',
'active' => true,
'info' => ['genre' => 'male', 'age' => 30]
];
$bulk = new \MongoDB\Driver\BulkWrite;
$_id1 = $bulk->insert($document);
$result = $manager->executeBulkWrite('database_name.collection_name', $bulk);
Update a document
Example for updating all documents where name is equal to "John":
$filter = ['name' => 'John'];
$document = ['name' => 'Mike'];
$bulk = new \MongoDB\Driver\BulkWrite;
$bulk->update(
$filter,
$document,
['multi' => true]
);
$result = $manager->executeBulkWrite('database_name.collection_name', $bulk);
<?php
// Connect to phpTester database
$manager = new MongoDB\Driver\Manager("mongodb://php-tester:123@localhost:27017/phpTester");
// Insert a document
$bulkWrite=new MongoDB\Driver\BulkWrite;
$doc=array("title" => "PHP Tester", "description" => "Test PHP code online");
$bulkWrite->insert($doc);
$manager->executeBulkWrite('phpTester.example', $bulkWrite);
// Get all documents
$query = new MongoDB\Driver\Query(array());
$cursor = $manager->executeQuery('phpTester.example', $query);
// Convert to Array and print result
print_r($cursor->toArray());
// Delete all documents
$bulkDelete = new MongoDB\Driver\BulkWrite;
$bulkDelete->delete(['title' => 'PHP Tester' ], ['limit' => 0]);
$manager->executeBulkWrite('phpTester.example', $bulkDelete);
?>
相關文章
- PHP 操作 MongoDBPHPMongoDB
- php連線mongodbPHPMongoDB
- MongoDB結合PHPMongoDBPHP
- docker mlocati/docker-php-extension-installer安裝composerDockerPHP
- Your PHP installation appears to be missing the MySQL extension which is requirePHPAPPMySqlUI
- MongoDB changeStream 的 PHP 實現MongoDBPHP
- 面向 Extension 開發 ? Today Extension
- php_mongo.dll下載(php操作mongoDB需要)PHPMongoDB
- App ExtensionAPP
- Extension,CategoryGo
- PHP操作MongoDB(增刪改查)PHPMongoDB
- PHP 擴充套件 Mongo 與 MongoDBPHP套件MongoDB
- php + MongoDB + Sphinx 實現全文檢索PHPMongoDB
- Introduction to MongoDB for Java, PHP and Python DevelopersMongoDBJavaPHPPythonDeveloper
- Hack With Chrome ExtensionChrome
- mysqlalchemy audit extensionMySql
- Tampermonkey & Extension
- MongoDB PHP Driver的連線處理解析MongoDBPHP
- MongoDB+PHP聯合開發例項MongoDBPHP
- App extension 總結APP
- beegoapix - beego api extensionGoAPI
- chrome開發 extensionChrome
- IOS category 與 extensioniOSGo
- chrome extension/plugin pathChromePlugin
- App Extension Study 1APP
- Swift學習之路-ExtensionSwift
- 談談Category和ExtensionGo
- FSLib.Extension庫
- swiftUI-extension的使用SwiftUI
- rocky 安裝gnome extension
- SAP S/4HANA裡extension include view和extension view的區別View
- PHP-fpm MongoDB 連線數爆了問題PHPMongoDB
- Hybris做增強的兩種方式:In App Extension和Side by Side ExtensionAPPIDE
- PostgreSQL DBA(150) - Extension(pgmetrics)SQL
- PostgreSQL DBA(184) - Extension(hypoPG)SQL
- 【Java 8實戰】Extension MethodsJava
- PostgreSQL編寫自定義extensionSQL
- iOS Extension Category Protrol 例子理解iOSGo