php+mongo函数,php中mongoDB数据库扩展函数

1:首先需要去安装对应版本的mongoDB php扩展dll文件,开扩展,重启apache

这个可以去php官网找找,可以去php手册看看mongoDB函数,里面安装和函数使用的介绍,

2:

Mongo 配置选项

名字                                  默认                       可修改范围                    描述

mongo.default_host         "localhost"                PHP_INI_ALL

mongo.default_port          27017                      PHP_INI_ALL                 默认连接端口

mongo.auto_reconnect    true                          PHP_INI_SYSTEM         是否在丢失连接的时候重新连接

mongo.allow_persistent   true                          PHP_INI_SYSTEM          是否支持持久连接

mongo.chunk_size          262144                     PHP_INI_SYSTEM          每块的大小,至少100bytes,最大4 megabytes

mongo.cmd                    "$"                              PHP_INI_ALL                 在非同一类型比较时代替$

mongo.utf8                     "1"                              PHP_INI_ALL                1.1.0版本后此选项将淘汰

一般连接

$connection = new Mongo(); // connects to localhost:27017

$connection = new Mongo( "example.com" );

$connection = new Mongo( "example.com:65432" );

$connection ->close();关闭连接

给库添加账号密码验证

$m = new Mongo();

$db = $m->admin;

$db->authenticate($username, $password);

其它连接方式

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

//默认端口连上blog库

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

//连接到mongo和其副本上

$m = new Mongo("mongodb://localhost:27017,localhost:27018");

长连接,如下:x为任意标示字符,

$m = new Mongo("localhost:27017", array("persist" => "x"));

1000次短连接耗时18s,而长连接,只需要0.02秒,

socket连接方式,这个我没试验成功,应该只针对linux

$m = new Mongo("mongodb:///tmp/mongo-27017.sock");

$m = new Mongo("mongodb://username:password@/tmp/mongo-27017.sock:0/foo");

2:选着数据库

$db = $connection->dbname;//如果库不存在,会自己建立一个

3:选择collection

$collection = $db->foobar;//两种方法,如下。同理,不存在的话会新建

$collection = $connection->baz->foobar;

4:添加数据

$data = array("name" => "lxh","info" => "lxh info");

$collection->insert($data); 采用关联数组,

5:查找数据

$result = $collection->findOne(); //查找一条

$cursor = $collection->find(); //查找所有

他们都可以带参数,参数为关联数组,如下:

$query = array( "i" => 71 ); //多个条件,在数组中添加即可

$cursor = $collection->find( $query );

6:条件编写

多个条件,在数组中添加即可

$query = array( "i" => 71,"name" => "lxh" );

不等条件,大于,小于,大于等于,小于等于

$query = array( "id" => array( '$gt' => 50 ) );//查找id大于50的数据

$query = array(

"i" => array( "\$gt" => 20, "\$lte" => 30 )

);

//20 < i <= 30 大于等于估计用"\$gte"了,猜一下

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

为躲避$,我们可以在php.ini中设定  mongo.cmd = ":" 用:代替$

7:计数

echo $collection->count();

8:遍历结果

$cursor = $collection->find( $query );

//遍历结果,或用简单的遍历数组的方法

while( $cursor->hasNext() ) {

var_dump( $cursor->getNext() );

}

9:创建索引

$collection->ensureIndex( array( "i" => 1 ) ); //i递增索引

$collection->ensureIndex( array( "i" => -1, "j" => 1 ) );

// i递减索引,j递增索引

10:其它

利用添加返回的"_id"

$person = array("name" => "joe");

$people->insert($person);

$joe = $people->findOne(array("_id" => $person['_id']));

_id是mongoid。最常见的错误是attepting使用一个字符串匹配mongoid

$person = array("name" => "joe");

$people->insert($person);

$pid = $person['_id'] . "";

// 这样做是错误的

$joe = $people->findOne(array("_id" => $pid));

//保存

$collection->save(array("awards" => array("gold", "silver", "bronze")));

awards列,保存一个数组{ "_id" : ObjectId("4b06c282edb87a281e09dad9"), "awards" : ["gold", "silver", "bronze"]}

忽视awards是数组,条件查询

$cursor = $collection->find(array("awards" => "gold"));

{

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

"awards" :

[

{

"first place" : "gold"

},

{

"second place" : "silver"

},

{

"third place" :  "bronze"

}

]

}

如果是此类,查询如下:

$cursor = $collection->find(array("awards.first place" => "gold"));

//in 查询 gold or copper

$cursor = $collection->find(array("awards" => array('$in' => array("gold", "copper"))));

$collection->save( array("gold", "silver", "bronze"));

0列silver,1列 bronze

修改

$collection->update(

array("name" => "dfd"), //修改查询条件

array('$set' => array('sex' => "aim")),

array('multiple'=>true) //更改多行,否则只改一行

);

{

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

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

"comments" :

[

{

"author" : "Mike",

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

},

{

"author" : "John",

"comment" : "I disagree."

}

]

}

//知道索引

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

//如果不知道 索引,可以用$代替

$blog->update(

array("comments.author" => "John"),

array('$set' => array('comments.$.author' => "Jim")));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值