YII2操作mongodb笔记

本文介绍了如何在YII2框架中集成MongoDB,并通过示例展示了基本的操作流程,包括连接配置、数据插入、查询、更新等。

操作之前得保证已经安装了mongodb,windows下安装可参考另一篇博文:

http://my.oschina.net/chinahub/blog/479268

componets配置:

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

控制器:

<?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\Customer;
class MonController extends Controller
{
    public function actionIndex()
    {
        $collection = Yii::$app->mongodb->getCollection ( 'customer' );
        $res = $collection->insert ( [ 
        'name' => 'John Smith22',
        'status' => 2 
        ] );
        var_dump($res);
    }
    public function actionList()
    {
        $query = new Query ();
        $query->select ( [ 
        'name',
        'status' 
        ] )->from ( 'customer' )->offset ( 10 )->limit ( 10 );
        $rows = $query->all ();
        var_dump ( $rows );
    }
    public function actionView()
    {
        $query = new Query ();
        $row = $query->from ( 'customer' )->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' => Customer::find (),
        'pagination' => [ 
        'pageSize' => 10 
        ] 
        ] );
        $models = $provider->getModels ();
        var_dump ( $models );
    }
    public function actionQuery()
    {
        $query = new Query ();
        $query->from ( 'customer' )->where ( [ 
        'status' => 2 
        ] );
        $provider = new ActiveDataProvider ( [ 
        'query' => $query,
        'pagination' => [ 
        'pageSize' => 10 
        ] 
        ] );
        $models = $provider->getModels ();
        var_dump ( $models );
    }
    public function actionSave()
    {
        $res = Customer::saveInfo ();
        var_dump ( $res );
    }
}

模型:

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

YII2的mongodb拓展下载:

https://github.com/yiisoft/yii2-mongodb


中文网址:

http://www.runoob.com/

http://www.mongoing.com/

http://www.cnblogs.com/libingql/archive/2011/06/09/2076440.html

常用命令 ,同mysql,eg:

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

show users;

show dbs;

db.version();

db.stats();

use yiimongodb;

show collections;


转载于:https://my.oschina.net/chinahub/blog/546806

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值