新聞中心
PHP7 MongDB 安裝與使用
本文教程只適合在 PHP7 的環(huán)境,如果你是 PHP5 環(huán)境,你可以參閱 PHP MongDB 安裝與使用。

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供萬(wàn)年網(wǎng)站建設(shè)、萬(wàn)年做網(wǎng)站、萬(wàn)年網(wǎng)站設(shè)計(jì)、萬(wàn)年網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、萬(wàn)年企業(yè)網(wǎng)站模板建站服務(wù),十余年萬(wàn)年做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
PHP7 Mongdb 擴(kuò)展安裝
我們使用 pecl 命令來(lái)安裝:
$ /usr/local/php7/bin/pecl install mongodb
執(zhí)行成功后,會(huì)輸出以下結(jié)果:
…… Build process completed successfully Installing '/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/mongodb.so' install ok: channel://pecl.php.net/mongodb-1.1.7 configuration option "php_ini" is not set to php.ini location You should add "extension=mongodb.so" to php.ini
接下來(lái)我們打開(kāi) php.ini 文件,添加 extension=mongodb.so 配置。
可以直接執(zhí)行以下命令來(lái)添加。
$ echo "extension=mongodb.so" >> `/usr/local/php7/bin/php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
注意:以上執(zhí)行的命令中 php7 的安裝目錄為 /usr/local/php7/,如果你安裝在其他目錄,需要相應(yīng)修改 pecl 與 php 命令的路徑。
Mongodb 使用
PHP7 連接 MongoDB 語(yǔ)法如下:
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
插入數(shù)據(jù)
將 name 為"菜鳥(niǎo)教程" 的數(shù)據(jù)插入到 test 數(shù)據(jù)庫(kù)的 runoob 集合中。
new MongoDB\BSON\ObjectID, 'name' => '菜鳥(niǎo)教程'];
$_id= $bulk->insert($document);
var_dump($_id);
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.runoob', $bulk, $writeConcern);
?>
讀取數(shù)據(jù)
這里我們將三個(gè)網(wǎng)址數(shù)據(jù)插入到 test 數(shù)據(jù)庫(kù)的 sites 集合,并讀取迭代出來(lái):
insert(['x' => 1, 'name'=>'菜鳥(niǎo)教程', 'url' => 'http://www.runoob.com']);
$bulk->insert(['x' => 2, 'name'=>'Google', 'url' => 'http://www.google.com']);
$bulk->insert(['x' => 3, 'name'=>'taobao', 'url' => 'http://www.taobao.com']);
$manager->executeBulkWrite('test.sites', $bulk);
$filter = ['x' => ['$gt' => 1]];
$options = [
'projection' => ['_id' => 0],
'sort' => ['x' => -1],
];
// 查詢數(shù)據(jù)
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery('test.sites', $query);
foreach ($cursor as $document) {
print_r($document);
}
?>
輸出結(jié)果為:
stdClass Object
(
[x] => 3
[name] => taobao
[url] => http://www.taobao.com
)
stdClass Object
(
[x] => 2
[name] => Google
[url] => http://www.google.com
)
更新數(shù)據(jù)
接下來(lái)我們將更新 test 數(shù)據(jù)庫(kù) sites 集合中 x 為 2 的數(shù)據(jù):
update(
['x' => 2],
['$set' => ['name' => '菜鳥(niǎo)工具', 'url' => 'tool.runoob.com']],
['multi' => false, 'upsert' => false]
);
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern);
?>
接下來(lái)我們使用 "db.sites.find()" 命令查看數(shù)據(jù)的變化,x 為 2 的數(shù)據(jù)已經(jīng)變成了菜鳥(niǎo)工具:
刪除數(shù)據(jù)
以下實(shí)例刪除了 x 為 1 和 x 為 2的數(shù)據(jù),注意 limit 參數(shù)的區(qū)別:
delete(['x' => 1], ['limit' => 1]); // limit 為 1 時(shí),刪除第一條匹配數(shù)據(jù)
$bulk->delete(['x' => 2], ['limit' => 0]); // limit 為 0 時(shí),刪除所有匹配數(shù)據(jù)
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern);
?>
更多使用方法請(qǐng)參考:http://php.net/manual/en/book.mongodb.php。
本文題目:創(chuàng)新互聯(lián)MongoDB教程:PHP7MongDB安裝與使用
網(wǎng)址分享:http://m.fisionsoft.com.cn/article/djpedgg.html


咨詢
建站咨詢
