symfony使用mongodb

  • 参考文档

  • 安装package

     composer require mongodb/mongodb
    
  • DEMO

     <?php
     /**
      * 框架会自动加载类库无需require_once
      */
     require_once __DIR__ . '/vendor/autoload.php';
    
     use \MongoDB\Client;
    
     $client = new Client("mongodb://192.168.0.126:27017");
     $base = 'base';
     $coll = 'coll';
     $collection = $client->$base->$coll;
     /**
      * 选择collection另一种写法
      */
     //$collection = $client->selectDatabase($base)->selectCollection($entity);
     /**
      * 手动生成_id
      */
     $id = sprintf('%16.6f', microtime(true));
     $id = str_replace('.', '', $id);
     /**
      * 增一条
      */
     $result = $collection->insertOne(['_id' => $id, 'name' => 'TomCat', 'age' => 20]);
     echo "插入了一条数据" . PHP_EOL;
     $_id = $result->getInsertedId();
     echo $_id . PHP_EOL;
     /**
      * 查一条
      */
     $result = (array)$collection->findOne(['_id' => $_id]);
     echo "查到了一条数据" . PHP_EOL;
     print_r($result);
     /**
      * 查多条
      */
     $filter = [];//WHERE
     $options = [
         /**
          * 默认查询所有
          * _id字段: '_id' => 0或1
          * 主字段:'name' => 1
          * 子字段:'m.n' => 0或1
          * SELECT name, age
          */
         'projection' => ['_id' => 0, 'name' => 1, 'age' => 1],
         /**
          * 1:ORDER BY name ASC
          * -1:ORDER BY name DESC
          */
         'sort' => ['name' => 1],
         /**
          * 查询的起始数据指针
          * LIMIT 0,N(默认0:起点)
          */
         'skip' => 0,
         /**
          * 查询数据的最大数量
          * LIMIT 0,N(默认0:所有)
          */
         'limit' => 0,
     ];
     $result = (array)$collection->find($filter, $options)->toArray();
     foreach ($result as &$res) {
         $res = (array)$res;
     }
     echo "查到了多条数据" . PHP_EOL;
     print_r($result);
     /**
      * 查并改(功能:只改$update中提供的字段,返回改之前的数据)
      */
     $filter = ['_id' => $id];
     $update = ['$set' => ['name' => '耿志环']];
     $result = (array)$collection->findOneAndUpdate($filter, $update);
     echo "查到修改了一条数据" . PHP_EOL;
     print_r($result);
     /**
      * 改一条并返回修改Doc的数量
      */
     $filter = ['_id' => $_id];
     $update = ['$set' => ['name' => 'TomCat']];
     $result = $collection->updateOne($filter, $update)->getModifiedCount();
     echo "修改了最多一条数据" . PHP_EOL;
     print_r($result . PHP_EOL);
     /**
      * 改多条并返回修改Doc的数量
      */
     $filter = ['name' => 'TomCat'];
     $update = ['$set' => ['name' => '耿志环']];
     $result = $collection->updateMany($filter, $update)->getModifiedCount();
     echo "修改了最多N条数据(N >= 1)" . PHP_EOL;
     print_r($result . PHP_EOL);
     /**
      * 查并换(功能:将_id以外字段全部去掉换成$replace,返回换之前的数据)
      * 整条文档的格式都会变化
      */
     $filter = ['_id' => $id];
     $replace = ['name' => '耿志环', 'sex' => 'man'];
     $result = (array)$collection->findOneAndReplace($filter, $replace);
     echo "查询替换了一条数据" . PHP_EOL;
     print_r($result);
     /**
      * 换一条
      */
     $filter = ['_id' => $id];
     $replace = ['name' => 'TomCat', 'sex' => 'man'];
     $result = $collection->replaceOne($filter, $replace)->getModifiedCount();
     echo "替换了最多一条数据" . PHP_EOL;
     print_r($result . PHP_EOL);
     /**
      * 查并删(返回删之前的数据)
      */
     $filter = ['_id' => $id];
     $result = (array)$collection->findOneAndDelete($filter);
     echo "查询删除了一条数据" . PHP_EOL;
     print_r($result);
     /**
      * 删一条
      */
     $filter = ['_id' => $id];
     $result = $collection->deleteOne($filter)->getDeletedCount();
     echo "查询删除了最多一条数据" . PHP_EOL;
     print_r($result . PHP_EOL);
     /**
      * 删多条
      */
     $filter = ['name' => 'TomCat'];
     $result = $collection->deleteMany($filter)->getDeletedCount();
     echo "查询删除了最多N条数据(N >= 1)" . PHP_EOL;
     print_r($result . PHP_EOL);
    
@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2019-01-11 16:52:19

results matching ""

    No results matching ""