Mongodb的常用操作

参看手册,php官方的http://us2.php.net/manual/en/mongo.manual.php

也可以参看mongodb官方的教程

数据库连接

⑴默认格式

$m = new Mongo();

//这里采用默认连接本机的27017端口,当然你也可以连接远程主机如                   192.168.0.4:27017,如果端口是27017,端口可以省略

⑵标准连接

$m = new Mongo("mongodb://${username}:${password}@localhost");

实例:$m = new Mongo("mongodb://127.0.0.1:27017/admin:admin");

数据库的用户名和密码都是admin

数据库操作:

插入数据:

  

结果:

带条件的查询

查询 title为huaibei的字段

1 $query = array( "title" => "huaibei" );

2 $cursor = $collection->find( $query );      // 在$collectio集合中查找满足$query的文档

常用的SQL转化为mongodb的条件

mysql: id = 123

mongo: array('id'=>123)

mysql: name link '%bar%'

mongo: array('name' => new MongoRegex('/.*bar.*/i'))

mysql: where id > 10

mongo: array('id' => array('$gt' => 10))

mysql: where id >= 10

mongo: array('id' => array('$gte' => 10))

mysql: where id < 10

mongo: array('id' => array('$lt' => 10))

mysql: where id <= 10

mongo: array('id' => array('$lte' => 10))

mysql: where id > 1 and id < 10

mongo: array('id' => array('$gt' => 1,'$lt' => 10))

mysql: where id <> 10

mongo: array('id' => array('$ne' => 10))

mysql: where id in(123)

mongo: array('id' => array('$in' => array(1,2,3)))

mysql: where id not in(123)

mongo: array('id' => array('$nin' => array(1,2,3)))

mysql: where id = 2 or id = 9

mongo: array('id' => array('$or' => array(array('id'=>2),array('id'=>9))))

mysql: order by name asc

mongo: array('sort'=>array('name'=>1))

mysql: order by name desc

mongo: array('sort'=>array('name'=>-1))

mysql: limit 0,2

mongo: array('limit'=>array('offset'=>0,'rows'=>2))

mysql: select name,email

mongo: array('name','email')

mysql: select count(name)

mongo: array('COUNT') //注意:COUNT为大写

更详细的转换参考http://us2.php.net/manual/en/mongo.sqltomongo.php

注意事项

查询时,每个Object插入时都会自动生成一个独特的_id,它相当于RDBMS中的主键,用于查询时非常方便 (_id每一都不同,很像自动增加的id)
如:

1 <?php 
2  
3 $person = array("name" => "joe"); 
4  
5 $people->insert($person); 
6  
$joe = $people->findOne(array("_id" => $person['_id'])); 
8  
9 ?> 

  

数据更改

添加一个新字段

/** 原字段:
 * {"username" : "...", "password" : "...", "email" : "..."}
 */
$coll->update(array("username" => "joe"), array('$set' => array("twitter" => "@joe4153")));

/** 操作后的字段:
 * {"username" : "joe", "password" : "...", "email" : "...", "twitter" : "@joe4153"}
 */

更改字段值

/** 原的数据
 * {"username" : "...", "password" : "...", "email" : "..."}
 */
$coll->update(array("username" => "joe"), array("userId" => 12345, "info" => array(
    "name" => "joe", "twitter" => "@joe4153", "email" => "..."), "likes" => array()));

/** 操作后的数据:
 * {
 *     "userId" : 12345, 
 *     "info" : {
 *         "name" : "joe", 
 *         "twitter" : "@joe4153", 
 *         "email" : "..."
 *     },
 *     "likes" : []
 * }
 */

比较复杂的更新

更改author为john的名称

{

"_id" : ObjectId("4b06c282edb87a281e09dad9"),

"content" : "this is a blog post.",

"comments" :

[

{

"author" : "Mike",

"comment" : "I think that blah blah blah...",

},

{

"author" : "John",

"comment" : "I disagree."

}

]

}

操作:<?php

$blog->update(
    array("comments.author" => "John"), 
    array('$set' => array('comments.$.author' => "Jim")));

?>

删除一个数据库

$m -> dropDB("comedy");

列出所有可用数据库

$m->listDBs();   //无返回值

转载一部分mongodb常用的数据库方法

MongoDB中有用的函数:

创建一个MongoDB对象

http://us.php.net/manual/en/mongodb.construct.php

$mo = new Mongo();

$db = new MongoDB($mo,’dbname’);//通过创建方式获得一个MongoDB对象

删除当前DB

http://us.php.net/manual/en/mongodb.drop.php

$db = $mo->dbname;

$db->drop();

获得当前数据库名

http://us.php.net/manual/en/mongodb.–tostring.php

$db = $mo->dbname;

$db->_tostring();

选择想要的collection:

A:

$mo = new Mongo();

$coll = $mo->dbname->collname;//获得一个collection对象

B:

$db = $mo->selectDB(’dbname’);

$coll = $db->collname;

C:

$db = $mo->dbname;

$coll = $db->collname;

D:

$db = $mo->dbname;

$coll = $db->selectCollectoin(’collname’);//获得一个collection对象

插入数据(MongoCollection对象):

http://us.php.net/manual/en/mongocollection.insert.php

MongoCollection::insert(array $a,array $options)

array $a 要插入的数组

array $options 选项

safe 是否返回操作结果信息

fsync 是否直接插入到物理硬盘

例程:

$coll = $mo->db->foo;

$a = array(’a'=>’b');

$options = array(’safe’=>true);

$rs  =$coll->insert($a,$options);

$rs为一个array型的数组,包含操作信息

删除数据库中的记录(MongoCollection对象):

http://us.php.net/manual/en/mongocollection.remove.php

MongoCollection::remove(array $criteria,array $options)

array $criteria  条件

array $options 选项

safe 是否返回操作结果

fsync 是否是直接影响到物理硬盘

justOne 是否只影响一条记录

例程:

$coll = $mo->db->coll;

$c = array(’a'=>1,’s’=>array(’$lt’=>100));

$options = array(’safe’=>true);

$rs = $coll->remove($c,$options);

$rs为一个array型的数组,包含操作信息

更新数据库中的记录(MongoCollection对象):

http://us.php.net/manual/en/mongocollection.update.php

MongoCollection::update(array $criceria,array $newobj,array $options)

array $criteria  条件

array $newobj 要更新的内容

array $options 选项

safe 是否返回操作结果

fsync 是否是直接影响到物理硬盘

upsert 是否没有匹配数据就添加一条新的

multiple 是否影响所有符合条件的记录,默认只影响一条

例程:

$coll = $mo->db->coll;

$c = array(’a'=>1,’s’=>array(’$lt’=>100));

$newobj = array(’e'=>’f',’x'=>’y');

$options = array(’safe’=>true,’multiple’=>true);

$rs = $coll->remove($c,$newobj,$options);

$rs为一个array型的数组,包含操作信息

查询collection获得单条记录(MongoCollection类):

http://us.php.net/manual/en/mongocollection.findone.php

array MongoCollection::findOne(array $query,array $fields)

array $query 条件

array $fields 要获得的字段

例程:

$coll = $mo->db->coll;

$query = array(’s’=>array(’$lt’=>100));

$fields = array(’a'=>true,’b'=>true);

$rs = $coll->findOne($query,$fields);

如果有结果就返回一个array,如果没有结果就返回NULL

查询collection获得多条记录(MongoCollection类):

http://us.php.net/manual/en/mongocollection.find.php

MongoCursor MongoCollection::find(array $query,array $fields)

array $query 条件

array $fields 要获得的字段

例程:

$coll = $mo->db->coll;

$query = array(’s’=>array(’$lt’=>100));

$fields = array(’a'=>true,’b'=>true);

$cursor = $coll->find($query,$fields);

//排序

$cursor->sort(array('字段'=>-1));(-1倒序,1正序)
//跳过部分记录

$cursor->skip(100);跳过100行

//只显示部分记录

$cursor->limit(100);只显示100行

返回一个游标记录对象MongoCursor。

针对游标对象MongoCursor的操作(MongoCursor类):

http://us.php.net/manual/en/class.mongocursor.php

循环或的结果记录:

$cursor = $coll->find($query,$fields);

while($cursor->hasNext()){

$r = $cursor->getNext();

var_dump($r);

}

或者

$cursor = $coll->find($query,$fields);

foreache($cursor as $k=>$v){

var_dump($v);

}

或者

$cursor = $coll->find($query,$fields);

$array= iterator_to_array($cursor);

**************************************************************************

Mongodb的介绍安装常用操作都已经介绍完毕,其实感觉他就是mysql,现在你就可以进入你的开发之旅了,推荐使用这个作为后台日志系统数据库。

最新文章

  1. C++ std::map
  2. .NET应用架构设计—用户端的防腐层作用及设计
  3. Java-接口练习
  4. 09_IO流
  5. R之data.table速查手册
  6. maven eclipse 安装
  7. WPF listbox UI虚拟化
  8. 转:switch内部的变量定义问题(goto类似)
  9. Codeforces Gym 100531D Digits 暴力
  10. oracle连接和执行流程总结
  11. Google chrome的字体设置
  12. 禁止UINavigationController 左滑 返回的效果
  13. struts2_20140720
  14. SQLyog-12.4.2版下载,SQLyog最新版下载,SQLyog官网下载,SQLyog Download
  15. Linux实战案例(4)CentOS清除用户登录记录和命令历史方法
  16. PHP观察者模式与Yii2.0事件
  17. Git-gitblit-Tortoisegit 搭建Windows Git本地服务器
  18. ABP模块运行解析
  19. Pomelo分布式游戏服务器框架
  20. 简单ssh建立 (paramiko)

热门文章

  1. QT5:总结篇 控件集合
  2. Java集合系列之HashMap
  3. jquery实现密码强度检测
  4. Angular JavaScript内存溢出问题 (FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory)
  5. 4 SQL 数据更新
  6. 杭电 1596 find the safest road (最小路径变形求最大安全度)
  7. Huffman codes
  8. SSRS ( .rdl文件)如何动态的设置导出Excel文件中的工作表标签名
  9. Run-time Settings--General--Run Logic
  10. 最长链(codevs 1814)