1//插入:
2 //插入多行:
3//得到一个记录
4 //得到记录集合
5 //查找:
//更新;(全部)
条件操作符:
$gt : >
$lt : <
$gte: >=
$lte: <=
$ne : !=、<>
$in : in
$nin: not in
$all: all
$not: 反匹配(1.3.3及以上版本)
6//更新一个:
7//save,次_id存在则更新,不存在则插入。
8 //删除
9//yii\mongodb\Query 转换成 yii\mongodb\Connection;
$collection = Yii::$app->mongodb->getCollection('customer');
$collection->insert(['name' => 'John Smith', 'status' => 2]);
$collection->insert(['name' => 'tom', 'status' => 4,'s'=>['age'=>13,'sex'=>'man','class'=>4 ]]);
2 //插入多行:
Yii::$app->mongodb->getCollection('customer')->batchInsert([['name' => 'water', 'status' => 1,'s'=>['age'=>11,'sex'=>'man','class'=>1 ]],['name' => 'terry', 'status' => 1,'s'=>['age'=>11,'sex'=>'man','class'=>1 ]],['name' => 'tom', 'status' => 1,'s'=>['age'=>11,'sex'=>'man','class'=>1 ]]]);
3//得到一个记录
$customer = Yii::$app->mongodb->getCollection('customer')->findOne(['status'=>4]);
4 //得到记录集合
$collection = Yii::$app->mongodb->getCollection('customer')->find(['status'=>4]);
foreach($collection as $row){
echo "name:".$row['name']."<br/>";
echo $row['s']['age']?"---age:".$row['s']['age']."<br/>":"";
echo $row['s']['sex']?"---sex:".$row['s']['sex']."<br/>":"";
echo $row['s']['age']?"---age:".$row['s']['age']."<br/>":"";
echo "<hr>";
}
echo "end";
exit;
5 //查找:
use yii\mongodb\Query;
$query = new Query;
$query->from('customer')
->where(['status'=>['$gt'=>1],'name'=>'tom','s.age'=>['$gt'=>12]])
->limit(10);
$rows = $query->all();
foreach($rows as $row){
echo "name:".$row['name']."<br/>";
echo "status:".$row['status']."<br/>";
echo $row['s']['age']?"---age:".$row['s']['age']."<br/>":"";
echo $row['s']['sex']?"---sex:".$row['s']['sex']."<br/>":"";
echo $row['s']['age']?"---age:".$row['s']['age']."<br/>":"";
echo "<hr>";
}
//更新;(全部)
条件操作符:
$gt : >
$lt : <
$gte: >=
$lte: <=
$ne : !=、<>
$in : in
$nin: not in
$all: all
$not: 反匹配(1.3.3及以上版本)
Yii::$app->mongodb->getCollection('customer')->update(['status'=>1],['$set'=>['name'=>'ss']]);
6//更新一个:
Yii::$app->mongodb->getCollection('customer')->update(['status'=>1],['$set'=>['name'=>'ss']]);
7//save,次_id存在则更新,不存在则插入。
Yii::$app->mongodb->getCollection('customer')->save(['_id'=>'54220ddb247f8b9ad9548b456c','name' => 'terry', 'status' => 3,'s'=>['age'=>11,'sex'=>'man','class'=>1 ]]);
8 //删除
Yii::$app->mongodb->getCollection('customer')->remove(['status'=>1]);
9//yii\mongodb\Query 转换成 yii\mongodb\Connection;
$query ->select(['name', 'status'])
->from('customer')
->where(['status'=>['$gt'=>1]])
->getCollection()
;
10.循环查找并更新的例子:
$customer = Yii::$app->mongodb->getCollection('customer');
$collection = $customer->find(['status'=>2]);
foreach($collection as $row){
echo "name:".$row['name']."<br/>";
echo "_id:".$row['_id']."<br/>";
echo "status:".$row['status']."<br/>";
echo $row['s']['age']?"---age:".$row['s']['age']."<br/>":"";
echo $row['s']['sex']?"---sex:".$row['s']['sex']."<br/>":"";
echo $row['s']['age']?"---age:".$row['s']['age']."<br/>":"";
echo "<hr>";
$row['name'] = "eee";
$customer->save($row);
}