yii2下使用monggodb及其model生成

第一步,需要安装依赖库

php composer.phar require –prefer-dist yiisoft/yii2-mongodb

第二步,在配置文件中添加如下配置

return [

//….

‘components’ => [

‘mongodb’ => [

‘class’ => ‘\yii\mongodb\Connection’,

‘dsn’ => ‘mongodb://@localhost:27017/mydatabase’,

‘options’ => [

“username” => “Username”,

“password” => “Password”

]

],

],

];

第三步,继承yii\mongodb\ActiveRecord ,写一个满足自己的db连接基类

class SpiderActiveRecord extends yii\mongodb\ActiveRecord {

    public static function getDb()

    {

        return Yii::$app->get(‘spiderMongo’);

    }

}

第四步,model继承这个基类

实现collectionName,attributes 这几个方法,可以采用yii框架自动生成model,但是添加生成器的时候总报错

class ModelSpiderConf extends SpiderActiveRecord{

/**

* @inheritdoc

*/

public static function collectionName() {

return [‘apk_spider’, ‘spider_confs’];

}

/**

* @inheritdoc

*/

public function attributes()

{

return [

‘_id’,

‘start_page’,

‘conf_info’,

‘spider’,

‘steps’,

‘final_page’,

];

}

}

 

领附常用的查询语句:

./mongo ip:port/dbname -u mongo -p psw

查看当前选择的数据库 db
   查看数据库 show dbs
   查看集合  show collections

use 命令

MongoDB使用 use DATABASE_NAME 命令来创建数据库。如果指定的数据库DATABASE_NAME不存在,则该命令将创建一个新的数据库,否则返回现有的数据库。

语法

use DATABASE 语句的基本语法如下 –

use DATABASE_NAME
 创建数据表

db.createCollection(“Account”)

db.createCollection(“Test”,{capped:true, size:10000}) { “ok” : 1 }

{“ok”:1}

— 说明

capped:true,表示该集合的结构不能被修改;

size:在建表之初就指定一定的空间大小,接下来的插入操作会不断地按顺序APPEND数据在这个预分配好空间的文件中,如果已经超出空间大小,则回到文件头覆盖原来的数据继续插入。这种结构保证了插入和查询的高效性,它不允许删除单个记录,更新的也有限制:不能超过原有记录的大小。这种表效率很高,它适用于一些暂时保存数据的场合,比如网站中登录用户的session信息,又比如一些程序的监控日志,都是属于过了一定的时间就可以被覆盖的数据。

 

查(find)

  1. 左边是mongodb查询语句,右边是sql语句。对照着用,挺方便。
  2. users.find() select * from users
  3. users.find({“age” : 27}) select * from users where age = 27
  4. users.find({“username” : “joe”, “age” : 27}) select * from users where “username” = “joe” and age = 27
  5. users.find({}, {“username” : 1, “email” : 1}) select username, email from users
  6. users.find({}, {“username” : 1, “_id” : 0}) // no case // 即时加上了列筛选,_id也会返回;必须显式的阻止_id返回
  7. users.find({“age” : {“$gte” : 18, “$lte” : 30}}) select * from users where age >=18 and age <= 30 // $lt(<) $lte(<=) $gt(>) $gte(>=)
  8. users.find({“username” : {“$ne” : “joe”}}) select * from users where username <> “joe”
  9. users.find({“ticket_no” : {“$in” : [725, 542, 390]}}) select * from users where ticket_no in (725, 542, 390)
  1. users.find({“ticket_no” : {“$nin” : [725, 542, 390]}}) select * from users where ticket_no not in (725, 542, 390)
  2. users.find({“$or” : [{“ticket_no” : 725}, {“winner” : true}]}) select * form users where ticket_no = 725 or winner = true
  3. users.find({“id_num” : {“$mod” : [5, 1]}}) select * from users where (id_num mod 5) = 1
  4. users.find({“$not”: {“age” : 27}}) select * from users where not (age = 27)
  5. users.find({“username” : {“$in” : [null], “$exists” : true}}) select * from users where username is null // 如果直接通过find({“username” : null})进行查询,那么连带”没有username”的纪录一并筛选出来
  6. users.find({“name” : /joey?/i}) // 正则查询,value是符合PCRE的表达式
  7. food.find({fruit : {$all : [“apple”, “banana”]}}) // 对数组的查询, 字段fruit中,既包含”apple”,又包含”banana”的纪录
  8. food.find({“fruit.2” : “peach”}) // 对数组的查询, 字段fruit中,第3个(从0开始)元素是peach的纪录
  9. food.find({“fruit” : {“$size” : 3}}) // 对数组的查询, 查询数组元素个数是3的记录,$size前面无法和其他的操作符复合使用
  10. users.findOne(criteria, {“comments” : {“$slice” : 10}}) // 对数组的查询,只返回数组comments中的前十条,还可以{“$slice” : -10}, {“$slice” : [23, 10]}; 分别返回最后10条,和中间10条
  11. people.find({“name.first” : “Joe”, “name.last” : “Schmoe”}) // 嵌套查询
  12. blog.find({“comments” : {“$elemMatch” : {“author” : “joe”, “score” : {“$gte” : 5}}}}) // 嵌套查询,仅当嵌套的元素是数组时使用,
  13. foo.find({“$where” : “this.x + this.y == 10”}) // 复杂的查询,$where当然是非常方便的,但效率低下。对于复杂查询,考虑的顺序应当是 正则 -> MapReduce -> $where
  14. foo.find({“$where” : “function() { return this.x + this.y == 10; }”}) // $where可以支持javascript函数作为查询条件
  15. foo.find().sort({“x” : 1}).limit(1).skip(10); // 返回第(10, 11]条,按”x”进行排序; 三个limit的顺序是任意的,应该尽量避免skip中使用large-number

 

输出结果格式化,就加 .pretty(); 例如: db.spider_confs.find().pretty();

 



	

You May Also Like

About the Author: daidai5771

发表评论

电子邮件地址不会被公开。 必填项已用*标注