PHP的MongoDB驱动中文文档:https://www.php.cn/manual/vie...
PHP的MongoDB驱动官方文档:https://www.php.net/manual/zh...
有些细节都没写全,如想学习请阅读官方文档!
安装:https://blog.csdn.net/weixin_...
普通的增删改查与mysql驱动操作一样
增$rs = DB::collection('集合名字')->insertGetId($param);$rs = DB::collection('集合名字')->insert($param);删$rs = DB::collection('集合名字')->where(["_id"=>1])->delete();改$rs = DB::collection('集合名字')->where(["_id"=>1])->update(['name'=<'遗失的美好']);查$result = DB::collection('集合名字')->where(["_id"=>1])->get();// 分页函数$results = DB::collection('集合名字')->paginate(10);// 这样也能做到分页 还有排序 orderBy() 排序函数,skip() 跳过几条 limit() 限制取出几条$rs = DB::collection('集合名字')->orderBy('age','asc')->skip(10))->limit(10)->get();
对内嵌数组进行增删改,首先要知道MongoDB操作符:https://www.runoob.com/mongodb/mongodb-operators.html
增// 插入数组 $addToSet 插入时如果已经存在相同的则不插入 $rs = DB::collection('user_files_folder')->update(['$addToSet'=>['child'=>['name'=>'二傻子']])// 插入数组 $push 插入时如果已经存在相同的仍然插入$rs = DB::collection('user_files_folder')->update(['$push'=>['child'=>['name'=>'二傻子']])删$rs = DB::collection('user_files_folder')->update(['$pull'=>['child'=>['name'=>'二傻子']])改// 注意,这where条件如果需要多重结构则可以用.连接$set是修改操作符,对应修改对象(数组)修改对象如果需要多重结构也可以用.连接,这里的$符号代表对应的键(因为这里的数据结构是一个数组,$代表对应的序号,也可以写死成 0-9之类的数字)DB::collection('stock')->where(['info.id'=>"118b110212"])->update([ $set'=>[ 'info.$.id'=>"118b110jkjkjk" ]]);