nutter-nut/mongodb-sqlquery
使用SQL风格的Mongodb
v1.0.3-beta
2020-06-30 15:45 UTC
Requires
- php: >=7.2.0
- mongodb/mongodb: ^1.6
This package is auto-updated.
Last update: 2024-09-29 06:02:45 UTC
README
使用SQL风格的Mongodb
- 配置:在Laravel的config/database.php中添加设置
'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_DB_HOST', '127.0.0.1'),
'port' => env('MONGO_MONGO_DB_PORT', 27017),
'database' => env('MONGO_DB_DATABASE', 'marcompany'),
'username' => env('MONGO_MONGO_DB_USERNAME', 'maradmin'),
'password' => env('MONGO_DB_PASSWORD', 'password'),
'options' => [
'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'),
],
],
- 以下是一个使用Laravel进行SQL查询风格示例
- 创建模型 - 在Laravel根项目中使用命令
php artisan make:model UserDbModel
,并在顶部插入use Nantaburi\Mongodb\MongoNativeDriver\Model
- 创建模型 - 在Laravel根项目中使用命令
use Nantaburi\Mongodb\MongoNativeDriver\Model
class UserDbModel extends Model {
protected $collection = "users" ;
protected $database = "marcompany" ;
}
- 创建Laravel控制器
- 在Laravel根项目中使用命令
php artisan make:controller --model=Userdatabase
- 然后编辑并插入基本的SQL示例:
select * from user where username like 'suphacha%' and age > 18 or mooban = 'Pangpoi' ;
- 如下所示使用SQL转换
- 在Laravel根项目中使用命令
use App\UserDbModel ;
$users= UserDbModel::query()
->where("username" , "like" , "suphacha%" )
->andwhere("age" ,">", 18)
->orwhere("mooban" ,"=" ,"Pangpoi" )
->get() ;
return view('userlist')->with("users",$users) ;
- 向集合中插入数据
- 模型文件在app/UserModel.php
<?php
namespace App;
use Nantaburi\Mongodb\MongoNativeDriver\Model as NanModel ;
class UserModel extends NanModel
{
/*
* @override $collection to all stack extends back to -> Class Model -> Class Connection( Using)
*
*/
protected $collection = "users" ;
protected $database = "customer" ;
protected $fillable = [ "username","email","first_name","last_name","password",
"plan","services","server-reference","client-address",
"server-req-time"
];
}
- 控制器
- 以下是一个插入准备代码示例
- 如果字段数据不在fillable成员中,则插入将拒绝并出现错误
$prepairinsertServices["username"] = $request->input('username') ;
$prepairinsertServices["email"] = $request->input('email') ;
$prepairinsertServices["first_name"] = $request->input('first_name') ;
$prepairinsertServices["last_name"] = $request->input('last_name') ;
$prepairinsertServices["password"] = $request->input('psswd') ;
$prepairinsertServices["plan"] = $request->input('radioplan') ;
$prepairinsertServices["services"] = [ ] ;
// Get data from Check box
if ( null != $request->input('service-ecom') )
array_push ( $prepairinsertServices["services"] ,[ "service-ecom" , $request->input('service-ecom') ]) ;
if ( null != $request->input('service-chat') )
array_push ( $prepairinsertServices['services'], ["service-chat", $request->input('service-chat')]);
if ( null != $request->input('service-email') )
array_push ( $prepairinsertServices['services'],["service-email" , $request->input('service-emai)') ]);
$prepairinsertServices["server-reference"] = $_SERVER['HTTP_REFERER'] ;
$prepairinsertServices["client-address"] = $_SERVER['REMOTE_ADDR'] ;
$prepairinsertServices["server-req-time"] = $_SERVER['REQUEST_TIME'] ;
$resultInsert = UserModel::insert( $prepairinsertServices ) ;
// Handle insert error !
if ( $resultInsert[0] == 0 ) {
return redirect()->back() ->with('alert', $resultInsert[1] );
}else { sleep(1) ; }
$users = UserModel::all() ;
return view('usermanage',compact('users') ) ;
}
- 在视图中处理插入错误
- 将以下脚本添加到您的视图文件.blade.php中
<script>
var msg = '{{Session::get('alert')}}';
var exist = '{{Session::has('alert')}}';
if(exist){
alert(msg);
}
</script>