mongodbupdate数组操作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mongodbupdate数组操作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4290字,纯文字阅读大概需要7分钟。
内容图文
前一篇文章说到了mongodb update 的字符操作,下面说一下mongodb update的数组操作,用的版本是mongodb2.6.3。 一,$美元符号,在update中,可理解为数组下标 例1 db.students.insert( //插入测试数据 [ {"_id" :6, "grades" : [ 80, 85, 90 ],"score":[10,4
前一篇文章说到了mongodb update 的字符操作,下面说一下mongodb update的数组操作,用的版本是mongodb2.6.3。
一,$美元符号,在update中,可理解为数组下标
例1
db.students.insert( //插入测试数据 [ {"_id" :6, "grades" : [ 80, 85, 90 ],"score":[10,40,54]}, {"_id" :7, "grades" : [ 88, 90, 92 ],"score":[100,30,51]} ] ); //把满足score大于90的grades,数组的第一个元素设置成88 db.students.update( { score: {$gt:90} }, { $set: { "grades.$" : 88 } } , { multi:true } );
相同功能php代码:
$where = array("score"=>array('$gt'=>70)); $param = array('$set'=>array('grades.$'=>"303")); $ismore = array("multiple" => true); $collection->update($where,$param,$ismore);
例2
db.test2.insert( { "content" : "this is a blog post.", "comments" : [ { "author" : "Mike", "comment" : "I think that blah blah blah...", }, { "author" : "John", "comment" : "I disagree." } ] } ); //查找名为Mike的记录,并且该人的名字改成tank db.test2.update( { "comments.author": "Mike"}, { $set: { "comments.$.author" : "tank" } } );
相同功能php代码:
$where = array("comments.author"=>"John"); $param = array('$set'=>array('comments.$.author'=>"tank")); $ismore = array("multiple" => true); $collection->update($where,$param,$ismore);
二,$addToSet 如果数组中没有该数据,向数组中添加数据,如果该数组中有相同数组,不添加
db.test3.insert( {"_id" :6, "grades" : [ "aaa", "bbb", "ccc" ]} ); db.test3.update( { _id: 6 }, { $addToSet: { grades: "ddd" } });
相同功能php代码:
$where = array("_id"=>6); $param = array('$addToSet'=>array('grades'=>"eee")); $collection->update($where,$param);
三,$pop删除数组数据
db.test3.update( { _id: 6 }, { $pop: { grades: -1 } }); //从头删除 db.test3.update( { _id: 6 }, { $pop: { grades: 1 } }); //从尾删除
相同功能php代码:
$where = array("_id"=>6); $param = array('$pop'=>array('grades'=>-1)); $collection->update($where,$param);
四,$pull和$pullAll删除指定数据
1,$pull
> db.test3.find(); { "_id" : 6, "grades" : [ "ccc", "ddd" ] } { "_id" : 7, "grades" : [ "aaa", "bbb", "ccc" ] } { "_id" : 8, "grades" : [ "aaa", "bbb", "ccc", "ddd", "eee" ] } > db.test3.update( { grades: "aaa" }, { $pull: { grades: "aaa" } }, //支持这种查找或匹配 $pull: { votes: { $gte: 6 } } { multi: true } ); WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
相同功能php代码:
$where = array("grades"=>"bbb"); $param = array('$pull'=>array('grades'=>"bbb")); $ismore = array("multiple" => true); $collection->update($where,$param,$ismore);
2,$pullAll
db.students.update( { _id: {$gt:1} }, { $pullAll: { "grades": [90,92] } } //只支持数组 );
相同功能php代码:
$where = array("grades"=>"ddd"); $param = array('$pullAll'=>array('grades'=>array("ddd","eee"))); $ismore = array("multiple" => true); $collection->update($where,$param,$ismore);
五,$push,$each,$sort,$slice,$position
1,各元素解释
$push 向数组中添加元素
$each 循环数据
$sort 对数组进行排序
$slice 对整个collection表进行数据裁减,用的时候一定要当心
$position 插入数据的位置。
2,实例
db.test4.insert( { "_id" : 5, "quizzes" : [ { wk: 1, "score" : 10 }, { wk: 2, "score" : 8 }, { wk: 3, "score" : 5 }, { wk: 4, "score" : 6 } ] } ); db.test4.update( { _id: 5 }, { $push: { quizzes: { $each: [ { wk: 5, score: 8 }, { wk: 6, score: 7 }, { wk: 7, score: 6 } ], $sort: { score: -1 }, $slice: 3, $position:2 } } } );
相同功能php代码:
$where = array("_id"=>5); $param = array('$push'=>array('quizzes'=>array('$each'=>array(array("wk"=>9,"score"=>10),array("wk"=>20,"score"=>11)), '$sort'=>array("score"=>-1), '$position'=>2, '$slice'=>3 //用$slice一定要小心,在这里会把整表数据裁减成3条 ) ) ); $collection->update($where,$param);前一篇文章说到了mongodb update 的字符操作,下面说一下mongodb update的数组操作,用的版本是mongodb2.6.3。 一,$美元符号,在update中,可理解为数组下标 例1 db.students.insert( //插入测试数据 [ {"_id" :6, "grades" : [ 80, 85, 90 ],"score":[10,40,54]}, {"_id" :7, "grades" : [ 88, 90, 92 ],"score":[100,30,51]} ] ); //把满足score大于90的grades,数组的第一个元素设置成88 db.students.update( { score: {$gt:90} }, { $set: { "grades.$" : 88 } } [...]
内容总结
以上是互联网集市为您收集整理的mongodbupdate数组操作全部内容,希望文章能够帮你解决mongodbupdate数组操作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。