PHP操作MongoDB 数据库总结记录
作者:网络转载 发布时间:[ 2014/5/28 11:39:23 ] 推荐标签:PHP MongoDB 数据库
5.删除记录操作
|
//** 删除节点 **/
$where=array(‘column_name’=>’col685′);
$result=$collection->update($where,array(‘$unset’=>’column_exp’));
/**
* 删除节点column_exp
*/
/*
* *
* 完整格式:update(array $criteria, array $newobj [, array $options = array() ] )
* 注意:1.注意区分替换更新与修改更新
* 2.注意区分数据类型如 array(’91u’=>’684435′)与array(’91u’=>684435)
* *
*/
//** 清空表 **/
$collection->remove(); #清空集合
//** 删除指定MongoId **/
$id = new MongoId(“4d638ea1d549a02801000011″);
$collection->remove(array(‘_id’=>(object)$id));
|
6.查询数据记录
|
//** 统计表记录数 **/
echo ‘count:’.$collection->count().”<br>”; #全部
echo ‘count:’.$collection->count(array(‘type’=>’user’)).”<br>”; #统计‘type’ 为 user 的记录
echo ‘count:’.$collection->count(array(‘age’=>array(‘$gt’=>50,’$lte’=>74))).”<br>”; #统计大于50小于等于74
echo ‘count:’.$collection->find()->limit(5)->skip(0)->count(true).”<br>”; #获得实际返回的结果数
/**
* 注:$gt为大于、$gte为大于等于、$lt为小于、$lte为小于等于、$ne为不等于、$exists不存在
*/
//** 获取表中所有记录 **/
$cursor = $collection->find()->snapshot();
foreach ($cursor as $id => $value) {
echo “$id: “; var_dump($value); echo “<br>”;
}
/**
* 注意:
* 在我们做了find()操作,获得$cursor游标之后,这个游标还是动态的.
* 换句话说,在我find()之后,到我的游标循环完成这段时间,如果再有符合条件的记录被插入到collection,那么这些记录也会被$cursor 获得.
* 如果你想在获得$cursor之后的结果集不变化,需要这样做:
* $cursor = $collection->find();
*/
//** 查询一条数据 **/
$cursor = $collection->findOne();
/**
* 注意:findOne()获得结果集后不能使用snapshot(),fields()等函数;
*/
//** 设置显示字段 age,type 列不显示 **/
$cursor = $collection->find()->fields(array(“age”=>false,”type”=>false));
$cursor = $collection->find()->fields(array(“user”=>true)); //只显示user 列
/**
* 我这样写会出错:$cursor->fields(array(“age”=>true,”type”=>false));
*/
//** 设置条件 (存在type,age节点) and age!=0 and age<50 **/
$where=array(‘type’=>array(‘$exists’=>true),’age’=>array(‘$ne’=>0,’$lt’=>50,’$exists’=>true));
$cursor = $collection->find($where);
//** 分页获取结果集 **/
$cursor = $collection->find()->limit(5)->skip(0);
//** 排序 **/
$cursor = $collection->find()->sort(array(‘age’=>-1,’type’=>1)); ##1表示降序 -1表示升序,参数的先后影响排序顺序
//** 索引 **/
$collection->ensureIndex(array(‘age’ => 1,’type’=>-1)); #1表示降序 -1表示升序
$collection->ensureIndex(array(‘age’ => 1,’type’=>-1),array(‘background’=>true)); #索引的创建放在后台运行(默认是同步运行)
$collection->ensureIndex(array(‘age’ => 1,’type’=>-1),array(‘unique’=>true)); #该索引是的
/**
* ensureIndex (array(),array(‘name’=>’索引名称’,'background’=true,’unique’=true))
* 详见:http://www.php.net/manual/en/mongocollection.ensureindex.php
*/
//** 取得查询结果 **/
$cursor = $collection->find();
$array=array();
foreach ($cursor as $id => $value) {
$array[]=$value;
}
|
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
在测试数据库性能时,需要注意哪些方面的内容?测试管理工具TC数据库报错的原因有哪些?怎么解决?数据库的三大范式以及五大约束编程常用的几种时间戳转换(java .net 数据库)优化mysql数据库的几个步骤数据库并行读取和写入之Python实现深入理解数据库(DB2)缓冲池(BufferPool)国内三大云数据库测试对比预警即预防:6大常见数据库安全漏洞数据库规划、设计与管理数据库-事务的概念SQL Server修改数据库物理文件存在位置使用PHP与SQL搭建可搜索的加密数据库用Python写一个NoSQL数据库详述 SQL 中的数据库操作详述 SQL 中的数据库操作Java面试准备:数据库MySQL性能优化

sales@spasvo.com