欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在Laravel中使用MongoDB的方法示例

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

MongoDB實(shí)用場(chǎng)景

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

使用Homebrew在macoOS安裝MongoDB PHP Driver

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

$ 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

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

...
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)建一個(gè)ext-mongodb.ini文件

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

將mongodb.so擴(kuò)展寫入該文件

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

同時(shí)在php.ini中移除mongodb.so擴(kuò)展

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

重啟一下PHP

sudo brew service restart --all

查看是否安裝成功

php -m|grep mongodb

在Laravel中使用MongoDB

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

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

成功后,再安裝Laravel-MongoDB擴(kuò)展

composer require jenssegers/mongodb -vvv

按照擴(kuò)展文檔說(shuō)明,我們添加一個(gè)MongoDB數(shù)據(jù)庫(kù)連接

//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ù)庫(kù)

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

./mongo

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

show dbs;

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

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

use viewers;

使用數(shù)據(jù)庫(kù)后,需要?jiǎng)?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擴(kuò)展,可以基于Eloquent與Query Builder操作MySQL一樣的數(shù)據(jù)php artisan thinker

查詢ad_clicks集合所有記錄

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

查詢單個(gè)記錄

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

修改某個(gè)記錄

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

在Laravel ORM中查詢MongoDB

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

php artisan make:model Models/AdClick

修改繼承父類和數(shù)據(jù)庫(kù)連接,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)計(jì)訪問(wèn)數(shù)據(jù)

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

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

相關(guān)文章

最新評(píng)論