• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

mongodb update 数组 操作

mysql 搞代码 4年前 (2022-01-09) 20次浏览 已收录 0个评论

前一篇文章说到了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,数组的第一个元素设置成88db.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的记录,并且该人的名字改成tankdb.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<div style="color:transparent">本文来源gaodai.ma#com搞#代!码(网</div>: 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中,可理解为数组下标例1db.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,数组的第一个元素设置成88db.students.update( { score: {$gt:90} }, { $set: { “grades.$” : 88 } } […]


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:mongodb update 数组 操作

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址