一、laravel安装mongodb
1.先安装php7的mongodb扩展,安装好对应版本之后,查看php-info()如下表示安装成功
2.安装laravel的mongodb拓展
composer require jenssegers/mongodb
3.在config/app.php下注册服务
'providers' 中加入
Jenssegers\Mongodb\MongodbServiceProvider::class,
'aliases' 中加入
'Moloquent' => Jenssegers\Mongodb\Eloquent\Model::class,
'Mongo' => Jenssegers\Mongodb\MongodbServiceProvider::class,
config/database.php中配置数据库
'mongodb' => array( //MongoDB
'driver' => 'mongodb',
'host' => '1xx.1xx.1xx.1x', //数据库服务器的ip
'port' => 27017, //数据库服务器上mongodb服务对应的端口
'database' => 'task_manager', //数据库名称
'username' => 'user',
'password' => 'xxx',
'options' => array(
'database' => 'task_manager' // 要使用的数据库
)
),
方法一:modal
添加modal类
<?php
namespace App;
use Emadadly\LaravelUuid\Uuids;
use Jenssegers\Mongodb\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\SoftDeletes;
class ExampleModel extends Model
{
//
use Uuids;
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
use SoftDeletes;
protected $connection = 'mongodb';
protected $collection = 'xxx表名'; //表名
protected $primaryKey = 'id'; //设置主键
protected $fillable = [ 'id','title', 'type','xx']; //设置字段白名单
/**
* 需要被转换成日期的属性。
*
* @var array
*/
protected $dates = ['deleted_at'];
}
使用方法:
$result= ExampleModel ::where('id',$id)->get();
方法二:DB
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/6/27 0027
* Time: 上午 11:19
*/
namespace App;
use DB;
class Mongodb
{
public static function connectionMongodb($tables)
{
return $users = DB::connection('mongodb')->collection($tables);
}
}
控制器使用
$connection = Mongodb::connectionMongodb('xxx表名');
$result= $connection ->get();
二、laravel安装es
1.安装es composer require elasticsearch/elasticsearch ‘~7.0’
2…env文件中增加一行: ES_HOSTS=your/es/host
3.database增加
'elasticsearch' => [
// Elasticsearch 支持多台服务器负载均衡,因此这里是一个数组
'hosts' => explode(',', env('ES_HOSTS')),
]
4.修改 app/Providers/AppServiceProvider.php 文件
public function register()
{
$this->app->singleton('es',function()
{
$bulider=ElasticBuilder::create()->setHosts(config('database.elasticsearch.hosts'));
if(app()->environment()=='local')
{
//配置日志,Elasticsearch 的请求和返回数据将打印到日志文件中,方便我们调试
$bulider->setLogger(app('log'));
}
return $bulider->build();
});
}
控制器中:
$query = [];
$query[] = ['range'=>['date'=>['gte'=>$start.' 00:00:00','lte'=>$end.' 23:59:59']]];
$query[] = ['bool'=>['should'=>['must'=>]['range'=>['date'=>['gte'=>$start.' 00:00:00','lte'=>$end.' 23:59:59']]]]]]];
if($sort){
$sortBy = ['date'=>['order'=>$sort]];
}else{
$sortBy = [];
}
$params = [
'index' => 'es_api_log',
'body' => [
'query' => [
"bool"=>[
"must"=>$query
]
],
"from"=> 0,
"size"=> 1000,
'sort'=>$sortBy
],
'scroll' => '1m',
];
$lists = [];
// 汇总数据
if(($result = app('es')->search($params)) && ($res = $result['hits']['hits'])){
$lists = array_merge($lists,$res);
$s_params['scroll_id'] = $result['_scroll_id'];
$s_params['scroll'] = '1m';
while($res = app('es')->scroll($s_params)['hits']['hits']){
$lists = array_merge($lists,$res);
}
app('es')->clearScroll(['scroll_id'=>$result['_scroll_id']]);
}
三、yii安装mongodb 使用
componets配置:
'mongodb' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://test:123456@127.0.0.1:27017/yiimongodb',
],
控制器:
$collection = Yii::$app->mongodb->getCollection('test');
$res = $collection->insert([
'name'=>'XXX',
'date'=>'XXX',
] );