Mongodb 内嵌数组操作
2015-11-19 13:17
507 查看
转载地址:http://blog.51yip.com/nosql/1639.html
mongodb update 数组 操作
前一篇文章说到了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 javascript脚本案例
- 移植mongoDb中一个db中的collections到同一主机的另一db中
- 在C#中使用官方驱动操作MongoDB
- java对mongodb的基础操作(1)
- MongoDB 启动mongo不带DB
- MongoDB shell 格式化
- django mongodb init install
- MongoDB小结20 - find【查询条件$size】
- MongoDB小结19 - find【查询条件$all】
- MongoDB小结18 - find【查询条件$not】
- MongoDB小结17 - find【查询条件$or】
- MongoDB小结16 - find【查询条件$in】
- MongoDB C# / .NET Driver 中IMongoQuery的内部实现Query的用法
- mongoDB 基本操作,增删改查
- mongodb新手入门,mongodb命令学习
- MongoDB 知识要点一览
- Linux下MongoDB安装及使用
- MongoDB——聚合 && 游标
- mongodb基本使用
- Linux系统下安装MongoDB 指南