php mongodb execute,php简单操作mongodb
您現在的位置是:網站首頁>>PHP>>php
php簡單操作mongodb
發布時間:2019-09-29 16:34:25作者:wangjian瀏覽量:525點贊量:0
一:插入數據
例:向默認的test數據庫的wj表中插入數據$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
// 插入數據
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['id' => 1, 'name'=>'測試', 'url' => 'http://www.百度.com']);
$manager->executeBulkWrite('test.wj', $bulk);
2:查詢數據
查詢test數據庫的wj表中name值不為測試的數據$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$filter = ['name' => '測試'];
$options = [
'projection' => ['_id' => 0],
'sort' => ['name' => -1],
];
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery('test.wj', $query);
foreach ($cursor as $document) {
print_r($document);
}
3:更新數據
將test數據庫中的wj表中id為1的數據中name字段變為測試1234,$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
['id' => 1],
['$set' => ['name' => '測試1234']],
['multi' => false, 'upsert' => false] ?#multi表示只更新一條數據,upsert表示如果不存在update的記錄,不進行插入操作
);
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.wj', $bulk, $writeConcern);
4:刪除數據$bulk = new MongoDB\Driver\BulkWrite;
$bulk->delete(['id' => 1], ['limit' => 1]);? ?// limit 為 1 時,刪除第一條匹配數據
$bulk->delete(['id' => 2], ['limit' => 0]);? ?// limit 為 0 時,刪除所有匹配數據
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.wj', $bulk, $writeConcern);
0
+1
總結
以上是生活随笔為你收集整理的php mongodb execute,php简单操作mongodb的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php面向对象程序设计,PHP面向对象程
- 下一篇: 爷爷脑梗,医生建议做支架,但也有医生不建