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
- docker mlocati/docker-php-extension-installer安裝composerDockerPHP
- MongoDB changeStream 的 PHP 實現MongoDBPHP
- PHP操作MongoDB(增刪改查)PHPMongoDB
- Tampermonkey & Extension
- App ExtensionAPP
- php + MongoDB + Sphinx 實現全文檢索PHPMongoDB
- mysqlalchemy audit extensionMySql
- Hack With Chrome ExtensionChrome
- App extension 總結APP
- PHP-fpm MongoDB 連線數爆了問題PHPMongoDB
- Hybris做增強的兩種方式:In App Extension和Side by Side ExtensionAPPIDE
- rocky 安裝gnome extension
- swiftUI-extension的使用SwiftUI
- PostgreSQL DBA(150) - Extension(pgmetrics)SQL
- PostgreSQL DBA(184) - Extension(hypoPG)SQL
- SAP S/4HANA裡extension include view和extension view的區別View
- 從PHP客戶端看MongoDB通訊協議TDPHP客戶端MongoDB協議
- 推薦一個php7+ mongodb三方類PHPMongoDB
- Chrome Extension 訊息傳遞Chrome
- Unofficial Windows Binaries for Python Extension PackagesWindowsPythonPackage
- PostgreSQL編寫自定義extensionSQL
- 【Java 8實戰】Extension MethodsJava
- PHP操作MongoDB時的整數問題及對策UQPHPMongoDB
- PostgreSQL DBA(83) - Extension(pg_buffercache)SQL
- PostgreSQL DBA(84) - Extension(pg_prewarm)SQL
- PostgreSQL DBA(72) - Extension(pgplsql_check)SQL
- PostgreSQL DBA(162) - Extension(pg_catcheck)SQL
- PostgreSQL DBA(134) - Extension(auto_explain)SQLAI
- PostgreSQL DBA(130) - Extension(pgsql-gzip)SQL
- PostgreSQL DBA(133) - Extension(postgresql_anonymizer)SQL
- PostgreSQL DBA(63) - Extension(pg_qualstats)SQL
- PostgreSQL DBA(163) - Extension(pg_cron)SQL
- 正式釋出!Azure Functions OpenAPI ExtensionFunctionAPI
- iOS桌面小外掛 Widget ExtensioniOS
- 巧用 Class Extension 隱藏屬性
- CocoaPods工程使用外掛App ExtensionAPP