亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

在Laravel中使用MongoDB的方法示例

 更新時間:2019年11月11日 09:59:33   作者:會勇禾口王  
這篇文章主要介紹了在Laravel中使用MongoDB的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

MongoDB實用場景

  • 產(chǎn)品用戶訪問日志,點擊埋點統(tǒng)計信息
  • 業(yè)務(wù)系統(tǒng)環(huán)境參數(shù)配置信息
  • 業(yè)務(wù)系統(tǒng)運行時日志,如laravel.log,nginx.log

使用Homebrew在macoOS安裝MongoDB PHP Driver

在macOS中,MongoDB 擴展已經(jīng)從Homebrew倉庫中移除,需要通過pecl安裝此擴展。

$ sudo pecl install mongodb -v
...

Build process completed successfully
Installing '/usr/local/Cellar/php@7.2/7.2.19/pecl/20170718/mongodb.so'
install ok: channel://pecl.php.net/mongodb-1.5.4
Extension mongodb enabled in php.ini

在項目中,使用phpinfo() 查詢PHP擴展安裝位置。

...
Configuration File (php.ini) Path  /usr/local/etc/php/7.2
Loaded Configuration File  /usr/local/etc/php/7.2/php.ini
Scan this dir for additional .ini files  /usr/local/etc/php/7.2/conf.d
Additional .ini files parsed  /usr/local/etc/php/7.2/conf.d/ext-opcache.ini, /usr/local/etc/php/7.2/conf.d/php-memory-limits.ini
....

按照ext-opcache.ini配置,創(chuàng)建一個ext-mongodb.ini文件

touch /usr/local/etc/php/7.2/conf.d/ext-mongodb.ini

將mongodb.so擴展寫入該文件

 [mongodb]
 extension=/usr/local/Cellar/php@7.2/7.2.19/pecl/20170718/mongodb.so

同時在php.ini中移除mongodb.so擴展

extension="mongodb.so" // remove
extension="php_mongodb.so" // remove 

重啟一下PHP

sudo brew service restart --all

查看是否安裝成功

php -m|grep mongodb

在Laravel中使用MongoDB

使用Composer創(chuàng)建一個Laravel項目

composer create-project --prefer-dist laravel/laravel laravel-mongodb-exploer -vvv

成功后,再安裝Laravel-MongoDB擴展

composer require jenssegers/mongodb -vvv

按照擴展文檔說明,我們添加一個MongoDB數(shù)據(jù)庫連接

//database.php
...
'mongodb' => [
      'driver'  => 'mongodb',
      'host'   => env('MONGODB_HOST', 'localhost'),
      'port'   => env('MONGODB_PORT', 27017),
      'database' => env('MONGODB_DATABASE'),
      'username' => env('MONGODB_USERNAME'),
      'password' => env('MONGODB_PASSWORD'),
      'options' => [
        'database' => 'admin' // sets the authentication database required by mongo 3
      ]
    ],
...
 
 
//.env
... 
MONGODB_HOST=127.0.0.1
MONGODB_PORT=27017
MONGODB_DATABASE=viewers
...

命令行創(chuàng)建MongoDB數(shù)據(jù)庫

macOS中,在命令行執(zhí)行mongo開啟MongoDB Shell

./mongo

使用show dbs查看已有數(shù)據(jù)庫

show dbs;

admin  0.000GB
config  0.000GB
local  0.000GB
viewers 0.000GB

如果沒有發(fā)現(xiàn)viewers,則創(chuàng)建該數(shù)據(jù)庫。注意只有viewers中存在collection時, 上面結(jié)果才會顯示viewers

use viewers;

使用數(shù)據(jù)庫后,需要創(chuàng)建colleciton

db.ad_clicks.insert({"ip":"201.35.63.14", "ad_index": 3, "created_at": "2019-06-10 11:34:12"})

使用find查詢記錄

> db.ad_clicks.find()
{ "_id" : ObjectId("5cf71b34e14620598643d23b"), "ip" : "201.34.46.3", "ad_index" : "2", "created_at" : "2019-06-05 11:34:53" }
{ "_id" : ObjectId("5cf71d3de14620598643d23d"), "ip" : "200.14.145.64", "ad_index" : 1, "created_at" : "2019-06-04 11:11:45" }
{ "_id" : ObjectId("5cf71d3ee14620598643d23e"), "ip" : "200.14.145.64", "ad_index" : 1, "created_at" : "2019-06-04 11:11:45" }
{ "_id" : ObjectId("5cf71d44e14620598643d23f"), "ip" : "200.14.145.64", "ad_index" : 1, "created_at" : "2019-06-04 11:11:45" }
{ "_id" : ObjectId("5cf71d45e14620598643d240"), "ip" : "200.14.145.64", "ad_index" : 1, "created_at" : "2019-06-04 12:34:12" }
{ "_id" : ObjectId("5cfe28823316506991c41786"), "ip" : "201.35.63.14", "ad_index" : 3, "created_at" : "2019-06-10 11:34:12" }

在Laravel DB中查詢MongoDB

使用了Laravel-MongoDB擴展,可以基于Eloquent與Query Builder操作MySQL一樣的數(shù)據(jù)php artisan thinker

查詢ad_clicks集合所有記錄

DB::connection('mongodb')->table('ad_clicks')->get()

查詢單個記錄

DB::connection('mongodb')->collection('ad_clicks')->find('5cf71b34e14620598643d23b')

修改某個記錄

DB::connection('mongodb')->collection('ad_clicks')->where('_id', '5cf71b34e14620598643d23b')->update(['ad_index'=>2]);

在Laravel ORM中查詢MongoDB

在項目中,創(chuàng)建一個Model

php artisan make:model Models/AdClick

修改繼承父類和數(shù)據(jù)庫連接,AdClick.php

...
use Jenssegers\Mongodb\Eloquent\Model;

class AdClick extends Model
{
  protected $connection = 'mongodb';
 
   /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  protected $fillable = [];

  /**
   * The attributes that aren't mass assignable.
   *
   * @var array
   */
  protected $guarded = [];
}

繼續(xù)在Thinker中,插入數(shù)據(jù)

App\Models\AdClick::create(['ip' => '31.42.4.14', 'ad_index' => 4, 'created_at' => '2019-06-10 18:10:01', 'ip2long' => ip2long('31.42.4.14')]);

統(tǒng)計訪問數(shù)據(jù)

App\Models\AdClick::where('ip', '31.42.4.14')->count()

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論