Mongodb 内嵌数组操作

来源:互联网 发布:2016最新人口普查数据 编辑:程序博客网 时间:2024/06/06 08:27

转载地址:http://blog.51yip.com/nosql/1639.html

mongodb update 数组 操作


前一篇文章说到了mongodb update 的字符操作,下面说一下mongodb update的数组操作,用的版本是mongodb2.6.3。

一,$美元符号,在update中,可理解为数组下标

例1

查看复制打印?
  1. db.students.insert(       //插入测试数据  
  2.  [  
  3.  {"_id" :6, "grades" : [ 80, 85, 90 ],"score":[10,40,54]},  
  4.  {"_id" :7, "grades" : [ 88, 90, 92 ],"score":[100,30,51]}  
  5.  ]  
  6. );  
  7.   
  8. //把满足score大于90的grades,数组的第一个元素设置成88  
  9. db.students.update(  { score: {$gt:90} },  
  10.             { $set: { "grades.$" : 88 } } ,  
  11.             { multi:true }  
  12.              );  

相同功能php代码:

查看复制打印?
  1. $where = array("score"=>array('$gt'=>70));  
  2. $param = array('$set'=>array('grades.$'=>"303"));  
  3. $ismore = array("multiple" => true);  
  4. $collection->update($where,$param,$ismore);  

例2

查看复制打印?
  1. db.test2.insert(  
  2.  {  
  3.  "content" : "this is a blog post.",  
  4.  "comments" :  
  5.  [  
  6.  {  
  7.  "author" : "Mike",  
  8.  "comment" : "I think that blah blah blah...",  
  9.  },  
  10.  {  
  11.  "author" : "John",  
  12.  "comment" : "I disagree."  
  13.  }  
  14.  ]  
  15.  }  
  16. );  
  17.   
  18. //查找名为Mike的记录,并且该人的名字改成tank  
  19. db.test2.update( { "comments.author""Mike"},  
  20.  { $set: { "comments.$.author" : "tank" } }  
  21.  );  

相同功能php代码:

查看复制打印?
  1. $where = array("comments.author"=>"John");  
  2. $param = array('$set'=>array('comments.$.author'=>"tank"));  
  3. $ismore = array("multiple" => true);  
  4. $collection->update($where,$param,$ismore);  

二,$addToSet 如果数组中没有该数据,向数组中添加数据,如果该数组中有相同数组,不添加

查看复制打印?
  1. db.test3.insert(  
  2.  {"_id" :6, "grades" : [ "aaa""bbb""ccc" ]}  
  3.  );  
  4.   
  5. db.test3.update( { _id: 6 }, { $addToSet: { grades: "ddd"  } });  

相同功能php代码:

查看复制打印?
  1. $where = array("_id"=>6);  
  2. $param = array('$addToSet'=>array('grades'=>"eee"));  
  3. $collection->update($where,$param);  

三,$pop删除数组数据

查看复制打印?
  1. db.test3.update( { _id: 6 }, { $pop: { grades: -1 } }); //从头删除   
  2.   
  3. db.test3.update( { _id: 6 }, { $pop: { grades: 1 } }); //从尾删除  

相同功能php代码:

查看复制打印?
  1. $where = array("_id"=>6);  
  2. $param = array('$pop'=>array('grades'=>-1));  
  3. $collection->update($where,$param);  

四,$pull和$pullAll删除指定数据

1,$pull

查看复制打印?
  1. > db.test3.find();  
  2. "_id" : 6, "grades" : [ "ccc""ddd" ] }  
  3. "_id" : 7, "grades" : [ "aaa""bbb""ccc" ] }  
  4. "_id" : 8, "grades" : [ "aaa""bbb""ccc""ddd""eee" ] }  
  5.   
  6. > db.test3.update(  
  7.  { grades: "aaa" },  
  8.  { $pull: { grades: "aaa" } }, //支持这种查找或匹配 $pull: { votes: { $gte: 6 } }  
  9.  { multi: true }  
  10.  );  
  11. WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })  

相同功能php代码:

查看复制打印?
  1. $where = array("grades"=>"bbb");  
  2. $param = array('$pull'=>array('grades'=>"bbb"));  
  3. $ismore = array("multiple" => true);  
  4. $collection->update($where,$param,$ismore);  

2,$pullAll

查看复制打印?
  1. db.students.update( { _id: {$gt:1} },  
  2.  { $pullAll: { "grades": [90,92] } } //只支持数组  
  3.  );  

相同功能php代码:

查看复制打印?
  1. $where = array("grades"=>"ddd");  
  2. $param = array('$pullAll'=>array('grades'=>array("ddd","eee")));  
  3. $ismore = array("multiple" => true);  
  4. $collection->update($where,$param,$ismore);  

五,$push,$each,$sort,$slice,$position

1,各元素解释

$push 向数组中添加元素

$each 循环数据

$sort 对数组进行排序

$slice 对整个collection表进行数据裁减,用的时候一定要当心

$position 插入数据的位置。

2,实例

查看复制打印?
  1. db.test4.insert(  
  2. {  
  3.  "_id" : 5,  
  4.  "quizzes" : [  
  5.  { wk: 1, "score" : 10 },  
  6.  { wk: 2, "score" : 8 },  
  7.  { wk: 3, "score" : 5 },  
  8.  { wk: 4, "score" : 6 }  
  9.  ]  
  10. }  
  11. );  
  12.   
  13. db.test4.update( { _id: 5 },  
  14.  { $push: { quizzes: { $each: [ { wk: 5, score: 8 },  
  15.                                 { wk: 6, score: 7 },  
  16.                                 { wk: 7, score: 6 } ],  
  17.                        $sort: { score: -1 },  
  18.                        $slice: 3,  
  19.                        $position:2  
  20.                       }  
  21.            }  
  22.  }  
  23.  );  

相同功能php代码:

查看复制打印?
  1. $where = array("_id"=>5);  
  2. $param = array('$push'=>array('quizzes'=>array('$each'=>array(array("wk"=>9,"score"=>10),array("wk"=>20,"score"=>11)),  
  3.                                                '$sort'=>array("score"=>-1),  
  4.                                                '$position'=>2,  
  5.                                                '$slice'=>3        //用$slice一定要小心,在这里会把整表数据裁减成3条  
  6.                                               )  
  7.                                    )  
  8.                        );  
  9. $collection->update($where,$param);
     
0 0