MongoDB實(shí)用場(chǎng)景
- 產(chǎ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ù)中移除,需要通過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
查看是否安裝成功
在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ò)展文檔說明,我們添加一個(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
使用show dbs查看已有數(shù)據(jù)庫(kù)
show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
viewers 0.000GB
如果沒有發(fā)現(xiàn)viewers,則創(chuàng)建該數(shù)據(jù)庫(kù)。注意只有viewers中存在collection時(shí), 上面結(jié)果才會(huì)顯示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ì)訪問數(shù)據(jù)
App\Models\AdClick::where('ip', '31.42.4.14')->count()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- MongoDB 數(shù)據(jù)庫(kù)的命名、設(shè)計(jì)規(guī)范詳解
- 28個(gè)MongoDB經(jīng)典面試題詳解
- MongoDB常用數(shù)據(jù)庫(kù)命令大全
- 修復(fù) Mac brew 安裝 mongodb 報(bào) Error: No available formula with the name ‘mongodb’ 問題詳解
- MongoDB啟動(dòng)報(bào)錯(cuò) 28663 Cannot start server
- Node.js操作MongoDB數(shù)據(jù)庫(kù)實(shí)例分析
- MongoDB數(shù)據(jù)庫(kù)安裝配置、基本操作實(shí)例詳解
- Windows10安裝MongoDB4.0詳細(xì)步驟及啟動(dòng)配置教程
- nodejs對(duì)mongodb數(shù)據(jù)庫(kù)的增加修刪該查實(shí)例代碼
- mongodb基本命令實(shí)例小結(jié)
- Win10 64位安裝MongoDB數(shù)據(jù)庫(kù)的詳細(xì)教程
- linux下安裝mongodb教程
- Python操作redis和mongoDB的方法
- dotnet core鏈接mongodb代碼實(shí)例
- Zabbix3.4監(jiān)控mongodb數(shù)據(jù)庫(kù)狀態(tài)的方法
- Windows安裝壓縮版MongoDB的教程
- 關(guān)于MongoDB謹(jǐn)防索引seek的效率問題詳析
- MongoDB中數(shù)據(jù)的替換方法實(shí)現(xiàn)類Replace()函數(shù)功能詳解