美文网首页
php7 mongodb使用

php7 mongodb使用

作者: 浮生若梦_10dd | 来源:发表于2017-10-15 15:03 被阅读0次

    一、连接

          $url = mongodb://用户名:密码@ip:端口/库           //适多连接库为admin,操作时再切库

          $mongo=new\MongoDB\Driver\Manager($url);

    二、普通索引

           创建:

              $cmd=array(

                  'createIndexes'=>$collectionName,//集合名

                  'indexes'=>array(

                      array(

                            'name'=>$indexName,//索引名

                            'key'=>$indexKeys,//索引字段数组 $indexKeys
     = array( 'name' => 1, 'age' => 1 );

                           'unique'=>false,

                      )

                 ),

            );

            $command = new\MongoDB\Driver\Command($cmd);

            $result=$mongo->executeCommand(库名, $command);

            $response= current($result->toArray());

            if($response->ok==1){

                 return true;

            }else{

                  return false;

             }

           删除索引:

              $cmd=array(

                   'dropIndexes'=>$collectionName,//集合名

                   'index'=>$indexName,

              );

              $command = new\MongoDB\Driver\Command($cmd);

              $result = $mongo->executeCommand(库名, $command);

              $response = current($result->toArray());

              if($response->ok==1){

                   return true;

              }else{

                   return false;

              }

    三、增删改查

           

                $bulkWrite = new\MongoDB\Driver\BulkWrite(['ordered'=>true]);

                $dbCollectionName = 库名 + '.' + 集合名

                $bulkWrite->insert($data);//$data为待插入数据数组

                $mongo->executeBulkWrite($dbCollectionName,$bulkWrite);

             改:

                $bulkWrite = new\MongoDB\Driver\BulkWrite(['ordered'=>true]);

                $writeConcern = new\MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY,1000);

                $dbCollectionName = 库名 + '.' + 集合名

                $bulkWrite->update(

                     $where,//更改条件

                     $updateData,//待更改数据数组

                    $extra,//额外信息 $extra=array('multi'=>false,'upsert'=>false)

                 );

                $mongo->executeBulkWrite($dbCollectionName,$bulkWrite,$writeConcern);

             查:

                $options=array();

               if(!empty($skip)){

                    $options['skip'] = intval($skip);//$skip查询起始位置,等同于mysql的limit起始位置

                }


               if(!empty($limit)){

                    $options['limit'] = intval($limit);//查询条数

                }

                if(!empty($sort)){

                    $options['sort'] =$sort;//排序  $sort=array('_id'=>-1),_id有默认索引,1升序   -1倒序

                }

                //或查询示例

               //$where = array('$or' => array(array('number'=>2),array('number'=>9)));

               $query = new\MongoDB\Driver\Query($where,$options);

               $dbCollectionName = 库名 + '.' + 集合名

               $mongo->executeQuery($dbCollectionName,$query)->toArray();

             删:

                $bulkWrite = new\MongoDB\Driver\BulkWrite(['ordered'=>true]);

                $writeConcern = new\MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY,1000);

                $dbCollectionName = 库名 + '.' + 集合名

                $bulkWrite->delete($where,$extra);//$extra=array('limit'=>1)

                $mongo->executeBulkWrite($dbCollectionName,$bulkWrite,$writeConcern);

             统计总数:

                 $cmd = array(

                     'count'=>$collectionName,

                     'query'=>$where

                 );

                 $command = new\MongoDB\Driver\Command($cmd);

                 $result = $mongo->executeCommand(self::getDbName(),$command);

                 $response= current($result->toArray());

                 if($response->ok==1){

                     return$response->n;

                  }

                  return0;

               

    相关文章

      网友评论

          本文标题:php7 mongodb使用

          本文链接:https://www.haomeiwen.com/subject/wdhcuxtx.html