MongoDB在ThinkPHP里面怎么进行数据库操作

2025-04-16 03:55:19
推荐回答(1个)
回答1:

连接数据库

$conn=new Mongo(“mongodb://sa:123@localhost”); #带用户名密码

选择数据库和集合

$db=$conn->selectDB(“mydb”); 

$collection = $db->selectCollection(‘column’);

增删改查

1.插入

$array=array(‘column_name’=>’col’.rand(100,999),’column_exp’=>’xiaocai’);

$result=$collection->insert($array); #简单插入

2. 修改更新 

        $where=array(‘column_name’=>’col123′);

        $newdata=array(‘column_exp’=>’GGGGGGG’,'column_fid’=>444);

        $result=$collection->update($where,array(‘$set’=>$newdata));

        3.删除

        $where=array(‘column_name’=>’col685′);

        $result=$collection->update($where,array(‘$unset’=>’column_exp’));

        4.查询

         $result =  $collection->find();以后这类问题不会的话你可以去后盾人上面看看哦⊙∀⊙!⊙∀⊙!⊙∀⊙!⊙∀⊙!里面有教学视频