多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
有个composer包 composer require "mongodb/mongodb=^1.0.0@beta" https://github.com/mongodb/mongo-php-library laravel的包 composer require jenssegers/mongodb http://www.jianshu.com/p/841e05db7f97 https://laravel-china.org/articles/2560/using-mongodb-in-laravel # 连接 ~~~ $username='username'; $passwd='passwd'; $host='127.0.0.1'; $port='27017'; $dbb='dbname'; $mongodb = new MongoDB\Driver\Manager("mongodb://{$username}:{$passwd}@{$host}:$port/$dbb"); ~~~ 还有种 ~~~ $ip = "127.0.0.1"; $port = '27017'; $dns = 'mongodb://'.$ip.':'.$port; $manager = new \MongoClient($dns); var_dump($manager); ~~~ # 插入数据 将 name 为"菜鸟教程" 的数据插入到 test 数据库的 runoob 集合中。 ~~~ <?php $bulk = new MongoDB\Driver\BulkWrite; $document = ['_id' => new MongoDB\BSON\ObjectID, 'name' => '菜鸟教程']; $_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); ?> ~~~ # 读取数据 这里我们将三个网址数据插入到 test 数据库的 sites 集合,并读取迭代出来: ~~~ <?php $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017"); // 插入数据 $bulk = new MongoDB\Driver\BulkWrite; $bulk->insert(['x' => 1, 'name'=>'菜鸟教程', '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], ]; // 查询数据 $query = new MongoDB\Driver\Query($filter, $options); $cursor = $manager->executeQuery('test.sites', $query); foreach ($cursor as $document) { print_r($document); } ?> ~~~ 输出结果为: ~~~ stdClass Object ( [x] => 3 [name] => taobao [url] => http://www.taobao.com ) stdClass Object ( [x] => 2 [name] => Google [url] => http://www.google.com ) ~~~ # 更新数据 接下来我们将更新 test 数据库 sites 集合中 x 为 2 的数据: ~~~ <?php $bulk = new MongoDB\Driver\BulkWrite; $bulk->update( ['x' => 2], ['$set' => ['name' => '菜鸟工具', '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); ?> ~~~ 接下来我们使用 "db.sites.find()" 命令查看数据的变化,x 为 2 的数据已经变成了菜鸟工具: # 删除数据 以下实例删除了 x 为 1 和 x 为 2的数据,注意 limit 参数的区别: <?php $bulk = new MongoDB\Driver\BulkWrite; $bulk->delete(['x' => 1], ['limit' => 1]); // limit 为 1 时,删除第一条匹配数据 $bulk->delete(['x' => 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.sites', $bulk, $writeConcern); ?>