提示:PHP的mongodb扩展,从5.6开始已经废弃了原来的mongo扩展,PHP7的pecl下载地址:https://pecl.php.net/package/mongodb,或者选择mongodb官方开发:https://github.com/mongodb/mongo-php-library,git上的这个支持以前mongo写法,pecl上的mongodb不支持以前的写法。
下面用PHP7新的API总结一下:
一:CURD
1:链接
<?php //链接mongodb $manager = new MongoDB\Driver\Manager('mongodb://root:123@10.10.10.104:27017');
root 用户;123:密码;如果没有密码则不写
2:查询
<?php //链接mongodb $manager = new MongoDB\Driver\Manager('mongodb://root:sjhc168@10.10.10.104:27017'); //查询 $filter = ['user_id'=>['$gt'=>0]]; //查询条件 user_id大于0 $options = [ 'projection' => ['_id' => 0], //不输出_id字段 'sort' => ['user_id'=>-1] //根据user_id字段排序 1是升序,-1是降序 ]; $query = new MongoDB\Driver\Query($filter, $options); //查询请求 $list = $manager->executeQuery('location.box',$query); // 执行查询 location数据库下的box集合 foreach ($list as $document) { print_r($document); }
查询更多条件使用方法,参考第二节mongodb基本命令,查询
3:添加
<?php //链接mongodb $manager = new MongoDB\Driver\Manager('mongodb://root:sjhc168@10.10.10.104:27017'); $bulk = new MongoDB\Driver\BulkWrite; //默认是有序的,串行执行 //$bulk = new MongoDB\Driver\BulkWrite(['ordered' => flase]);//如果要改成无序操作则加flase,并行执行 $bulk->insert(['user_id' => 2, 'real_name'=>'中国',]); $bulk->insert(['user_id' => 3, 'real_name'=>'中国人',]); $manager->executeBulkWrite('location.box', $bulk); //执行写入 location数据库下的box集合
4:修改
<?php //链接mongodb $manager = new MongoDB\Driver\Manager('mongodb://root:sjhc168@10.10.10.104:27017'); $bulk = new MongoDB\Driver\BulkWrite; //默认是有序的,串行执行 //$bulk = new MongoDB\Driver\BulkWrite(['ordered' => flase]);//如果要改成无序操作则加flase,并行执行 $bulk->update( ['user_id' => 2], ['$set'=>['real_name'=>'中国国'] ]); //$set相当于mysql的 set,这里和mysql有两个不同的地方, //1:字段不存在会添加一个字段; //2:mongodb默认如果条件不成立,新增加数据,相当于insert //如果条件不存在不新增加,可以通过设置upsert //db.collectionName.update(query, obj, upsert, multi); $bulk->update( ['user_id' => 5], [ '$set'=>['fff'=>'中国国'] ], ['multi' => true, 'upsert' => false] //multi为true,则满足条件的全部修改,默认为true,如果改为false,则只修改满足条件的第一条 //upsert为 treu:表示不存在就新增 ); $manager->executeBulkWrite('location.box', $bulk); //执行写入 location数据库下的box集合
ordered 设置
1:默认是ture,按照顺序执行插入更新数据,如果出错,停止执行后面的,mongo官方叫串行。
2:如果是false,mongo并发的方式插入更新数据,中间出现错误,不影响后续操作无影响,mongo官方叫并行
5:删除
<?php //链接mongodb $manager = new MongoDB\Driver\Manager('mongodb://root:sjhc168@10.10.10.104:27017'); $bulk = new MongoDB\Driver\BulkWrite; //默认是有序的,串行执行 //$bulk = new MongoDB\Driver\BulkWrite(['ordered' => flase]);//如果要改成无序操作则加flase,并行执行 $bulk->delete(['user_id'=>5]);//删除user_id为5的字段 $manager->executeBulkWrite('location.box', $bulk); //执行写入 location数据库下的box集合
delete还可以通过limit设置不同删除方式
$bulk->delete(['user_id' => 1], ['limit' => 1]); // limit 为 1 时,删除第一条匹配数据 $bulk->delete(['user_id' => 2], ['limit' => 0]); // limit 为 0 时,删除所有匹配数据,默认删除所有
6:捕获异常
MongoDB\Driver\Exception\AuthenticationException MongoDB\Driver\Exception\BulkWriteException MongoDB\Driver\Exception\ConnectionException MongoDB\Driver\Exception\ConnectionTimeoutException MongoDB\Driver\Exception\Exception //所有驱动程序异常的公共接口 MongoDB\Driver\Exception\ExecutionTimeoutException MongoDB\Driver\Exception\InvalidArgumentException MongoDB\Driver\Exception\LogicException MongoDB\Driver\Exception\RuntimeException MongoDB\Driver\Exception\SSLConnectionException MongoDB\Driver\Exception\UnexpectedValueException MongoDB\Driver\Exception\WriteException
说明:http://php.net/manual/zh/class.mongodb-driver-exception-authenticationexception.php
Atas ialah kandungan terperinci php7的mongodb基本用法(代码详解). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

Video Face Swap
Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Artikel Panas

Alat panas

MinGW - GNU Minimalis untuk Windows
Projek ini dalam proses untuk dipindahkan ke osdn.net/projects/mingw, anda boleh terus mengikuti kami di sana. MinGW: Port Windows asli bagi GNU Compiler Collection (GCC), perpustakaan import yang boleh diedarkan secara bebas dan fail pengepala untuk membina aplikasi Windows asli termasuk sambungan kepada masa jalan MSVC untuk menyokong fungsi C99. Semua perisian MinGW boleh dijalankan pada platform Windows 64-bit.

SublimeText3 versi Inggeris
Disyorkan: Versi Win, menyokong gesaan kod!

SublimeText3 Linux versi baharu
SublimeText3 Linux versi terkini

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Muat turun versi mac editor Atom
Editor sumber terbuka yang paling popular
