<?php
//安装参照 http://www.cnblogs.com/huangxincheng/archive/2012/02/18/2356595.html
//链接到远程主机的自定义的端口
$conn = new MongoClient("mongodb://127.0.0.1:27017/");
//选择数据库
$db = $conn->cx;
//选择数据集,相当于表
$collection = $db->log;
$do=$_GET['do'];
//查看数据库列表
if($do=='dblist'){
var_dump($conn->listDBs());
}
//添加数据
if($do=='insert'){
$doc = array(
"name" => "33222",
"type" => "database",
"count" => 1,
"versions" => array('0.9.7', '0.9.8', '0.9.9')
);
$collection->insert($doc);//插入数据
}
//查找一条数据
if($do=="selectone"){
$obj = $collection->findOne();//查找第一行
print_r($obj);
$obj = $collection->findOne(array("name"=>"ddd"));//查找指定一行
print_r($obj);
}
if($do=="selectall"){
$cursor = $collection->find();
$cursor = $collection->find(array("count"=>array('$gt'=>1,'$lte'=>74)))->limit(2)->fields(array("type"=>true,"name"=>true));
//->sort(array(‘age’=>-1,’type’=>1)); ##1表示降序 -1表示升序,参数的先后影响排序顺序
foreach ($cursor as $id => $value) {
echo "$id: ";
print_r( $value );
}
}
//计算行数
if($do=="count"){
echo $collection->count();
echo $collection->count(array("name"=>"ddd"));
}
//修改
if($do=="update"){
$where=array('count'=>3);
$newdata=array('type'=>'www2','count'=>43);
$result=$collection->update($where,array('$set'=>$newdata),array('multiple'=>true)); #$set:让某节点等于给定值,类似的还有$pull $pullAll $pop $inc,在后面慢慢说明用法
}
//替换
if($do=="replace"){
$where=array('name'=>'qqq');
$newdata=array('type'=>'234','count'=>3);
$result=$collection->update($where,$newdata);
}
//删除
if($do=='remove'){
$where=array('count'=>134);
$collection->remove($where);
}