1. 程式人生 > >MongoDB客戶端操作與PHP操作Mongodb

MongoDB客戶端操作與PHP操作Mongodb

一.獲取數量
db.books.count();
db.books.find().count();
db.books.count({'author':'張黎'});
db.books.find({'author':'張黎'}).count();
db.books.count({'money':{'$gt':60,'$lte':100}});
db.books.find({'money':{'$gt':60,'$lte':100}}).count();
對應的php程式碼
$num = $booksOrm->count();
$num = $booksOrm->find()->count();
$num = $booksOrm->count(array('author'=>'張黎'));
$num = $booksOrm->find(array('author'=>'張黎'))->count();
$num = $booksOrm->count(array('money'=>array('$gt'=>60,'$lte'=>100)));
$num = $booksOrm->find(array('money'=>array('$gt'=>60,'$lte'=>100)))->count();
二.查詢單條資料
db.books.findOne();
db.books.findOne({'title':'西遊記'});
對應的php程式碼:
$info = $booksOrm->findOne();
$info = $booksOrm->findOne(array('title'=>'西遊記'));
三.自定義欄位顯示
db.books.find({},{'title':1,'code':1}); //只顯示title和code欄位,_id預設顯示
db.books.find({},{'_id':0,'title':1,'code':1}); //只顯示title和code,_id也不顯示
db.books.find({'money':{'$gt':60,'$lte':100},'code':{'$gt':150}},{'_id':0,'title':1,'code':1});
對應的php程式碼:
$info = $booksOrm->find()->fields(array('title'=>1,'code'=>1));
$info = $booksOrm->find()->fields(array('_id'=>0,'title'=>1,'code'=>1));
$data = $booksOrm->find(array('money'=>array('$gt'=>60,'$lte'=>100),'code'=>array('$gt'=>150)))->fields(array('_id'=>0,'title'=>1,'code'=>1));
$info = array();
foreach($data as $bal){
	$info[] = $bal;
}
四.分頁--limit
db.books.find({},{'_id':0,'money':0,'code':0}).skip(3).limit(2); //跳過第x條,取n條 
對應的php程式碼:
$data = $booksOrm->find()->fields(array('_id'=>0,'money'=>0,'code'=>0))->skip(3)->limit(2);
五.排序--sort -1:desc 1:asc
db.books.find().sort({'money':1}); //根據money進行asc
db.books.find().sort({'money':-1}); //根據money進行desc
db.books.find().sort({'money':-1,'code':1}); //根據money進行desc,code進行asc
對應的php程式碼:
$data = $booksOrm->find()->sort(array('money'=>1));
$data = $booksOrm->find()->sort(array('money'=>-1));
$data = $booksOrm->find()->sort(array('money'=>1,'code'=>-1));
六.模糊查詢--like
db.books.find({'title':/王朝/}); // like '%王朝%'
db.books.find({'title':/^大/}); // like '大%'
db.books.find({'title':/王朝$/}); // like '%王朝%'
對應的php程式碼:
$param = array("title" => new MongoRegex('/王朝/'));
$param = array("title" => new MongoRegex('/^大/'));
$param = array("title" => new MongoRegex('/朝$/'));
$data = $booksOrm->find($param);
七.$in,$nin
db.books.find({'money':{'$in':[76,94]}});
對應的php程式碼:
$param = array("money" => array('$in'=>array(76,94))); 
$data = $booksOrm->find($param);
八.$or
db.books.find({'$or':[{'money':76},{'money':94}]});
對應的php程式碼:
$param = array('$or'=>array(array('money'=>76),array('money'=>94)));
$data = $booksOrm->find($param);
九.分組統計--group by
$option = array(
    //匹配條件
    array(
        '$match'=>array(
            'code'=>array(
                '$gt'=>80,
                '$lt'=>110
            )
         )
    ),
    //統計條件
    array(
        '$group'=>array(
            '_id'=>'$author',
            'numcount'=>array(
                '$sum'=>'$money'
             )
        )
    )
);
//獲取全部資料
$data = $booksOrm->aggregate($option);
$info = array();
foreach($data as $bal){
	$info[] = $bal;
}
十.distinct
db.books.distinct('author');
db.books.distinct('author',{'money':{'$gt':76,'$lte':110}});