通过 Laravel 使用 Moloquent 创建索引
Create Index using Moloquent with Laravel
我是 MongoDB 的新手。
我正在使用 Jensegger/Laravel-MongoDB Moloquent 功能来处理 Mongo 数据库。
我正在尝试用这种方法创建一个集合的索引:-
Schema::collection('events', function ($table) {
$table->index(['location' => '2dsphere']);
});
但是,我收到错误:-
Class Jenssegers\Mongodb\Schema' not found
我也添加了这两个:-
use Jenssegers\Mongodb\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
我有一个控制器方法如下:-
public function fetchMongoTest(Request $request){
$error = FALSE;
$respond = array();
$detail = array();
$err_message = array();
$err_validation = array();
$api_code = 2001;
try
{
if ($request->isMethod('post'))
{
$latitude = (float)$request->latitude;
$longitude = (float)$request->longitude;
$status = 1;
$mongoData = array();
$monTestObj = new Mongotest;
Schema::collection('events', function ($table) {
$table->index(['location' => '2dsphere']);
});
$monTestObj->location = ['type' => 'Point', 'coordinates' => [100.0, 0.0]];
$monTestObj->save();
$users = MongoTest::where('loc', 'near', [
'$geometry' => [
'type' => 'Point',
'coordinates' => [
$longitude,
$latitude
]
],
'$maxDistance' => 10,
]);
foreach($users as $u)
{
print_r($u->name);
}
}
else
{
$status = 0;
$message = Config::get('customConfig.api_messages.ENG.post_request_mandatory');
$err_message[] = $message;
}
}
catch(Exception $e)
{
$status = 0;
echo $e->getMessage(); die;
$message=Config::get('customConfig.api_messages.ENG.exception_error');
}
$response['status'] = $status;
$response['message'] = $message;
$response['details'] = $detail;
$response['error'] = $err_message;
$response['error_validation_key'] = $err_validation;
$response['api_version'] = $this->api_version;
$response['api_code'] = $api_code;
$respond['fetch-activity-list-prime'] = $response;
$jsonResult = json_encode($respond);
header('Content-Type: application/json; charset=utf-8');
echo $jsonResult ;
exit();
}
如何检查集合是否存在,如果不存在,则创建一个新集合?
编辑:
这是我的Mongo测试模型:-
<?php
namespace App\Http\Model;
//use Illuminate\Database\Eloquent\Model;
use Moloquent;
class MongoTest extends Moloquent
{
protected $connection = 'mongodb';
protected $collection = 'test';
//protected $collection = 'rh_country_help_text';
}
您似乎从某处得到了部分答案。 Schema
应该取自 "Larvel Migration",这是在您的应用程序中实际定义索引的一种推荐方法。
流程将设置为:
创建迁移
php artisan make:migration create_location_index
然后更改结构以添加 up
和 down
以创建和删除索引:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateLocationIndex extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::connection('mongodb')->table('test', function (Blueprint $collection) {
$collection->index([ "loc" => "2dsphere" ]);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::connection('mongodb')->table('test', function (Blueprint $collection) {
$collection->dropIndex(['loc_2dsphere']);
});
}
}
然后您可以运行迁移为detailed within the documentation
如果您决定 运行 迁移过程之外的代码,那么获取 MongoDB\Collection
对象的备用句柄可以是:
DB::collection('test')->raw(function($collection) {
return $collection->createIndex([ 'loc' => '2dsphere' ])
}
尽管此代码不属于控制器,但无论您做什么。创建索引的代码只需要 运行 一次。通常 "once only" 在您的数据库部署中,但在每个应用程序启动时发出命令并没有什么坏处,但是它肯定会对每个请求造成伤害。所以不要把它放在那里。
我是 MongoDB 的新手。
我正在使用 Jensegger/Laravel-MongoDB Moloquent 功能来处理 Mongo 数据库。
我正在尝试用这种方法创建一个集合的索引:-
Schema::collection('events', function ($table) {
$table->index(['location' => '2dsphere']);
});
但是,我收到错误:-
Class Jenssegers\Mongodb\Schema' not found
我也添加了这两个:-
use Jenssegers\Mongodb\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
我有一个控制器方法如下:-
public function fetchMongoTest(Request $request){
$error = FALSE;
$respond = array();
$detail = array();
$err_message = array();
$err_validation = array();
$api_code = 2001;
try
{
if ($request->isMethod('post'))
{
$latitude = (float)$request->latitude;
$longitude = (float)$request->longitude;
$status = 1;
$mongoData = array();
$monTestObj = new Mongotest;
Schema::collection('events', function ($table) {
$table->index(['location' => '2dsphere']);
});
$monTestObj->location = ['type' => 'Point', 'coordinates' => [100.0, 0.0]];
$monTestObj->save();
$users = MongoTest::where('loc', 'near', [
'$geometry' => [
'type' => 'Point',
'coordinates' => [
$longitude,
$latitude
]
],
'$maxDistance' => 10,
]);
foreach($users as $u)
{
print_r($u->name);
}
}
else
{
$status = 0;
$message = Config::get('customConfig.api_messages.ENG.post_request_mandatory');
$err_message[] = $message;
}
}
catch(Exception $e)
{
$status = 0;
echo $e->getMessage(); die;
$message=Config::get('customConfig.api_messages.ENG.exception_error');
}
$response['status'] = $status;
$response['message'] = $message;
$response['details'] = $detail;
$response['error'] = $err_message;
$response['error_validation_key'] = $err_validation;
$response['api_version'] = $this->api_version;
$response['api_code'] = $api_code;
$respond['fetch-activity-list-prime'] = $response;
$jsonResult = json_encode($respond);
header('Content-Type: application/json; charset=utf-8');
echo $jsonResult ;
exit();
}
如何检查集合是否存在,如果不存在,则创建一个新集合?
编辑:
这是我的Mongo测试模型:-
<?php
namespace App\Http\Model;
//use Illuminate\Database\Eloquent\Model;
use Moloquent;
class MongoTest extends Moloquent
{
protected $connection = 'mongodb';
protected $collection = 'test';
//protected $collection = 'rh_country_help_text';
}
您似乎从某处得到了部分答案。 Schema
应该取自 "Larvel Migration",这是在您的应用程序中实际定义索引的一种推荐方法。
流程将设置为:
创建迁移
php artisan make:migration create_location_index
然后更改结构以添加 up
和 down
以创建和删除索引:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateLocationIndex extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::connection('mongodb')->table('test', function (Blueprint $collection) {
$collection->index([ "loc" => "2dsphere" ]);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::connection('mongodb')->table('test', function (Blueprint $collection) {
$collection->dropIndex(['loc_2dsphere']);
});
}
}
然后您可以运行迁移为detailed within the documentation
如果您决定 运行 迁移过程之外的代码,那么获取 MongoDB\Collection
对象的备用句柄可以是:
DB::collection('test')->raw(function($collection) {
return $collection->createIndex([ 'loc' => '2dsphere' ])
}
尽管此代码不属于控制器,但无论您做什么。创建索引的代码只需要 运行 一次。通常 "once only" 在您的数据库部署中,但在每个应用程序启动时发出命令并没有什么坏处,但是它肯定会对每个请求造成伤害。所以不要把它放在那里。