YII2操作MongoDB常用语句

PHP 投稿 88700 0 评论

YII2操作MongoDB常用语句

首先安装MongoDB扩展: 

pecl install mongodb

通过 composer 安装MongoDB 依赖包,然后在项目中 config 配置文件夹的 main-local.php 中进行 componets配置:

'mongodb' => [
    'class' => '\yii\mongodb\Connection',
    'dsn' => 'mongodb://test:123456@127.0.0.1:27017/testmongodb',
],

控制器代码示例:

<?php
namespace frontend\controllers;
use Yii;
use yii\helpers\Url;
use yii\mongodb\Query;
use yii\web\Controller;
use yii\data\ActiveDataProvider;
use frontend\models\Xiaoming;
class MonController extends Controller
{
    public function actionIndex()
    {
        $collection = Yii::$app->mongodb->getCollection ( 'xiaoming' );
        $res = $collection->insert ( [ 
        'name' => 'test mongdb',
        'status' => 1 
        ] );
        var_dump($res);
    }
    public function actionList()
    {
        $query = new Query ();
        $query->select ( [ 
        'name',
        'status' 
        ] )->from ( 'xiaoming' )->offset ( 10 )->limit ( 10 );
        $rows = $query->all ();
        var_dump ( $rows );
    }
    public function actionView()
    {
        $query = new Query ();
        $row = $query->from ( 'xiaoming' )->one ();
        echo Url::toRoute ( [ 
        'item/update',
        'id' => ( string ) $row ['_id'] 
        ] );
        var_dump ( $row ['_id'] );
        var_dump ( ( string ) $row ['_id'] );
    }
    public function actionFind()
    {
        $provider = new ActiveDataProvider ( [ 
        'query' => Xiaoming::find (),
        'pagination' => [ 
        'pageSize' => 10 
        ] 
        ] );
        $models = $provider->getModels ();
        var_dump ( $models );
    }
    public function actionQuery()
    {
        $query = new Query ();
        $query->from ( 'xiaoming' )->where ( [ 
        'status' => 2 
        ] );
        $provider = new ActiveDataProvider ( [ 
        'query' => $query,
        'pagination' => [ 
        'pageSize' => 10 
        ] 
        ] );
        $models = $provider->getModels ();
        var_dump ( $models );
    }
    public function actionSave()
    {
        $res = Xiaoming::saveInfo ();
        var_dump ( $res );
    }
}

模型代码示例:

<?php
namespace frontend\models;
use yii\mongodb\ActiveRecord;
class Xiaoming extends ActiveRecord
{
    public static function collectionName()
    {
        return 'xiaoming';
    }
    public function saveInfo()
    {
        $xiaoming = new Xiaoming ();
        $xiaoming->name = '555';
        $xiaoming->email = '666';
        $xiaoming->insert ();
        return $xiaoming;
    }
    public function attributes()
    {
        return [ 
        '_id',
        'name',
        'email',
        'address',
        'status' 
        ];
    }
}

联合查询:

public function actionSearch($project_id)
{
    #聚合条件
    $where_condition = [];
    $where_condition = ['org_id'=>['$in'=>$org_id]];
    $where_sex = ['sex'=>['$eq'=>$sex]];
    $where_condition = array_merge($where_condition,$where_sex);
    $where_age =['age'=>['$gte'=>$small_age,'$lte'=>$big_age]];
    $where_condition = array_merge($where_condition,$where_age);
    #mongodb 随机查询并取rand_num条
    $userInfo = OaUser::getCollection()->aggregate([
            ['$match'=>$where_condition],
            ['$project'=> [
                '_id'=>0,'age'=>1,'job_family_name'=>1,
                'org_id'=>1,'org_level'=>1,'person_id'=>1
                    ]
                ],
            ['$sample'=>['size'=>$rand_num]],
        ]);
        #根据org_lvel进行分组查询
        $userInfo = OaUser::getCollection()->aggregate([
            ['$match'=>$where_condition],
            ['$project'=>
                [    
                        '_id'=>0,'age'=>1,'job_family_name'=>1,
                        'org_id'=>1,'org_level'=>1,'person_id'=>1
                ]
            ],
            ['$group'=>['_id'=>'$org_level']],
        ]);
}

也可以参考官方文档https://github.com/yiisoft/yii2-mongodb。

常用命令行:

db.createUser({"user":"test","pwd":"123456","roles":["readWrite","dbAdmin"]})

show users;

show dbs;

db.version();

db.stats();

use testmongodb;

编程笔记 » YII2操作MongoDB常用语句

赞同 (79) or 分享 (0)
游客 发表我的评论   换个身份
取消评论

表情
(0)个小伙伴在吐槽