php操作mongo语法
MongoDB不支持多表查询
1、运行mongo服务
打开cmd,输入E:进入E盘。
输入cd E:\mongdb\bin 进入该目录下。
输入mongod --dbpath E:\mongodb\data //开启mongo服务,并将数据信息保存在data文件夹下.
不要关闭cmd,关闭即关闭mongo服务。
2、操作语法
连接数据库
<?php $conn = new Mongo();//实例 $db = $conn->mongo//选择数据库 $coll=$db->user //选择表
insert
$coll->insert(array(‘id‘=>48));//插入的是int型的48,查询的时候直接‘id‘=>48 $coll->insert(array(‘id‘=>‘48‘))//插入的字符型的48,查询的时候‘id‘=>‘48‘
delete
$coll->remove();// delete from user $coll->remove(array(‘id‘=>1));// delete from user where id=1
select
$coll->find();// select * from user $coll->findone();//select top 1 * from user $coll->find(array(‘id‘=>1),array(‘id‘=>1,‘name‘=>1));//select id,name from user where id=1 $coll->find(array(‘name‘=>new MongoRegex(‘/a/‘)));//select * from user where name like ‘%a%‘ $coll->find(array(‘name‘=>new MongoRegex(‘/^a/‘)));//select * from user where name like ‘a%‘ $coll->find(array(‘id‘=>array(‘$gt‘=>3)))->sort(array(‘id‘=>1));//select *from user where id>3 order by id asc $gt是大于,$lt是小于,desc是 sort(array(‘id‘=>-1)) $coll->count();//select count(*) from user $coll->find(array(‘$or‘=>array(‘id‘=>1;),array(‘name‘=>‘b‘)))//select * from user where id=1 or name=‘b‘ $coll->find()->limit(5)->skip(0)->sort(array(‘id‘=>1))//select * from user order by id asc limit 0,5; $db->command(array(‘distinct‘=>‘user‘,‘key‘=>‘name‘))//select distinct * from user
update
$coll->update(array("id"=>3),array(‘$set‘=>array(‘id‘=>1)));//update user set id=1 where id=3; $coll->update(array("id"=>3),array(‘$inc‘=>array(‘id‘=>2)));//update user set id=id+2 where id=3;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。