php操作mongodb

连接

$mongo = new MongoClient('localhost:27017');

获取数据库

$db = $mongo->lxj

获取文档

$collect = $db->runoob

$collect->insert(['name'=>'lxj','age'=>20]);

$collect->remove(['name'=>'lxj'])

$collect->update(['name'=>'lxj'],['age'=>22])                   // 将整条更新为['age'=>22]
$collect->update(['name'=>'luoxianjie'],['$set'=>['age'=>20]])  // 只更新字段age
$collect->update(['name'=>'jack'],['$inc'=>['age'=>2]])         // 更新age字段为age+2

$collect->find()                                                // 查询所有
$collect->findOne()                                             // 查询单条
$collect->find([],['name'=>1])                                  // 只返回name字段
$collect->find([],['name'=>-1])                                 // 返回除name以外的字段
$collect->find(["age"=>["$gt"=>12,"$lt"=>24]])                  // 返回年龄大于12小于24的记录
$collect->find(["age"=>["in"=>[23,24]]])                        // 返回年龄在23,24的记录
$collect->find(['age'=>['nin'=>[23,24]]])                       // 返回年龄不在23,24的记录
$collect->find(["$or"=>[['name'=>'luoxianjie'],['age'=>24]]])   // 取出年龄等于24或name为luoxianjie的数据
$collect->find()->limit(10)->skip(5)                            // 取出第6-10条记录
$collect->find()->count()                                       // 取出记录条数 

猜你喜欢

转载自blog.csdn.net/qq_39647045/article/details/82623737