寮步网站建设,沈阳做网站哪家好,my23777免费域名查询,公益组织网站源码下面常用的update操作#xff0c;用mongodb2.6.3版本测试的#xff0c;官方发布的稳定版本2.4#xff0c;建议用稳定版。 一#xff0c;upsert表示如果有数据就不插入#xff0c;没数据就插入 1#xff0c;命令行下 db.peoples.update( //查找name等于tank的用户... { na…下面常用的update操作用mongodb2.6.3版本测试的官方发布的稳定版本2.4建议用稳定版。 一upsert表示如果有数据就不插入没数据就插入 1命令行下 db.peoples.update( //查找name等于tank的用户... { name: tank },... {... _id:1,... name: An下面常用的update操作用mongodb2.6.3版本测试的官方发布的稳定版本2.4建议用稳定版。一upsert表示如果有数据就不插入没数据就插入1命令行下 db.peoples.update( //查找name等于tank的用户... { name: tank },... {... _id:1,... name: Andy,... rating: 10,... score: 10... },... { upsert: true } //如果没有就插入... );WriteResult({ nMatched : 0, nUpserted : 1, _id : 1 }) db.peoples.find();{ _id : 1, name : Andy, rating : 10, score : 10 } db.peoples.update(... { _id: 1 },... {... _id:1,... name: Andy,... rating: 10,... score: 10... },... { upsert: true }... );WriteResult({ nMatched : 1, nUpserted : 0 }) //有匹配数据就不做插入操作 db.peoples.find();{ _id : 1, name : Andy, rating : 10, score : 10 }2php upsert操作$collection-update(array(name zhang),array(_id2,nametank,rating10,score10),array(upsert true));print_r($collection-findOne());二$set 替换值1命令行下操作 db.peoples.find();{ _id : 1, name : Andy, rating : 10, score : 10 } db.peoples.update(... { _id: 1 },... {... $set: { rating: 18 }... }... );WriteResult({ nMatched : 1, nUpserted : 0 }) db.peoples.find();{ _id : 1, name : Andy, rating : 18, score : 10 }2php $set操作$where array(_id1);$param array($setarray(score100)); //注意此处的set必须为 单引号$collection-update($where,$param);print_r($collection-findOne());三$inc如果没有对应对段就直接赋值如果有在原来的值上加上该值1命令行下操作 db.peoples.find();{ _id : 1, name : Andy, rating : 28, score : 10 } db.peoples.update(... { _id: 1 },... {... $inc: { age: 30 }... }... );WriteResult({ nMatched : 1, nUpserted : 0 }) db.peoples.find();{ _id : 1, age : 30, name : Andy, rating : 28, score : 10 } //第一次加了一个字段 db.peoples.update(... { _id: 1 },... {... $inc: { age: 30 }... }... );WriteResult({ nMatched : 1, nUpserted : 0 }) db.peoples.find();{ _id : 1, age : 60, name : Andy, rating : 28, score : 10 } //第二次发现有这个字段就把值加上去了2php $inc操作$where array(_id1);$param array($incarray(age30));$collection-update($where,$param);print_r($collection-findOne());四$unset删除字段1命令行下操作 db.peoples.find();{ _id : 1, age : 120, name : Andy, rating : 28, score : 10 } db.peoples.update(... { _id: 1 },... {... $unset: { age: } //删除age字段... }... );WriteResult({ nMatched : 1, nUpserted : 0 }) db.peoples.find();{ _id : 1, name : Andy, rating : 28, score : 10 }2php $unset操作$where array(_id1);$param array($unsetarray(score));$collection-update($where,$param);print_r($collection-findOne());五$rename修改字段名称1命令行下操作 db.peoples.find();{ _id : 1, name : Andy, rating : 28 } db.peoples.update(... { _id: 1 },... { $rename: { name: firstname } } //将name改成firstname... );WriteResult({ nMatched : 1, nUpserted : 0 }) db.peoples.find();{ _id : 1, firstname : Andy, rating : 28 }2php $rename 操作$where array(_id1);$param array($renamearray(firstnamename)); //将firstname改成name$collection-update($where,$param);print_r($collection-findOne());六multi更新多条数据1,命令行下操作 db.peoples.find();{ _id : 1, name : Andy, rating : 28 }{ _id : 2, name : zhang, rating : 3, score : 5 }{ _id : 3, name : hao, rating : 30, score : 5 } db.peoples.update(... { rating: { $gt: 15 } },... { $inc: { score: 10 } },... { multi: true }... );WriteResult({ nMatched : 2, nUpserted : 0 }) db.peoples.find();{ _id : 1, name : Andy, rating : 28, score : 10 } //这条数据更新了{ _id : 2, name : zhang, rating : 3, score : 5 }{ _id : 3, name : hao, rating : 30, score : 15 } //这条数据更新了2php multi操作$where array(ratingarray($gt10));$param array($setarray(nametank1,rating50));$ismore array(multiple true);$collection-update($where,$param,$ismore);版本不一样功能多少会不一样例如我用的版本是2.6.3,$min和$max就用不了会报errmsg : Invalid modifier specified $min希望mongodb出一个稳定高版本。