博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
YII2操作mongodb笔记(转)
阅读量:7061 次
发布时间:2019-06-28

本文共 2153 字,大约阅读时间需要 7 分钟。

 

componets配置:

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

  

 

控制器:

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 ); }}

  

 

模型:

name = '111'; $customer->email = '222'; $customer->insert (); return $customer; } public function attributes() { return [ '_id', 'name', 'email', 'address', 'status' ]; }}

  

 

YII2的mongodb拓展下载:

 

中文网址:

常用命令 ,同mysql,eg:

db.createUser({"user":"test","pwd":"123456","roles":["readWrite","dbAdmin"]})show users;show dbs;db.version();db.stats();use yiimongodb;show collections;

  

 

转载于:https://www.cnblogs.com/sandea/p/5651046.html

你可能感兴趣的文章
在servlet中注入spring环境
查看>>
Android源代码编译——下载
查看>>
chrome误删书签恢复。
查看>>
CentOS部署yapi
查看>>
thinkphp 中where 多条件查询
查看>>
Djano restframework
查看>>
JDK8-废弃永久代(PermGen)迎来元空间(Metaspace)
查看>>
android开发第一天打电话发短信
查看>>
使用指南
查看>>
物理内存相关的三个数据结构
查看>>
深入解析.NET框架
查看>>
零基础入门学习python-----读书笔记(二)
查看>>
FTP与HTTP上传文件的对比
查看>>
CocosPods 每次install pod 都卡在analyzing
查看>>
java 多线程——同步 学习笔记
查看>>
JS中innerHTML、outerHTML、innerText 、outerText、value的区别与联系?
查看>>
Q:无法直接启动带有“类库输出类型”的项目,若要调试此项目,请在此解决方案中添加一个引用库项目的可执行项目,将这个可执行项目设置为启动项目...
查看>>
中国人民公安大学 网络对抗技术 实验一
查看>>
一起学Android之ToggleButton和Switch
查看>>
中译英5
查看>>