mongo 更新数组 键值对
2016-06-27 18:14
435 查看
{ "_id" : 1, "grades" : [ 80, 85, 90 ] } { "_id" : 2, "grades" : [ 88, 90, 92 ] } { "_id" : 3, "grades" : [ 85, 100, 90 ] }
To update 80 to 82 in the grades array
in the first document, use the positional $ operator if you do not know the position of the element
in the array:
如果不知道值在数组的下标,可以用$操作把数组中的 80 更新成82
db.students.update( { _id: 1, grades: 80 }, { $set: { "grades.$" : 82 } } )
{ _id: 4, grades: [ { grade: 80, mean: 75, std: 8 }, { grade: 85, mean: 90, std: 5 }, { grade: 90, mean: 85, std: 3 } ] }用$操作符 来更新 std 字段的内嵌文档
db.students.update( { _id: 4, "grades.grade": 85 }, { $set: { "grades.$.std" : 6 } } )
相关文章推荐
- beego使用migrate错误
- Go语言学习:Channel
- 研究Extension和Category的一个例子
- POJ 2195 Going Home (最小费用最大流)
- 论文笔记 | Going deeper with convolutions
- Django项目结构布局
- LightOJ 1030 Discovering Gold
- mongo的数据备份
- Poj 2723 Go Deeper【2-SAT-----Tarjan强连通+二分】
- 特殊矩阵——三对角矩阵(Tridiagonal Matrix)
- hdu_3966_Aragorn's Story(树链剖分裸题)
- hdu_3966_Aragorn's Story(树链剖分裸题)
- 【读书笔记】Zygote 和 System 进程的启动过程
- 谷雪梅 Google中国
- Go语言学习笔记1
- 贪婪算法(Greedy Algorithm)
- 课后作业静态网页制作-仿Argo官网
- GOF-23种设计模式——装饰模式(学习笔记)
- 深入理解go的slice和到底什么时候该用slice?
- pgoneproxy使用tcmalloc来管理内存