mongodb yii 数据库操作集合

本文介绍如何使用 Yii2 框架进行 MongoDB 数据的基本增删改查操作,包括单条记录和批量记录的处理方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1//插入:
   $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);		  
      } 




       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值