本文介绍 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);

 

原文: 海底苍鹰博客 (推荐