设为首页 加入收藏

TOP

MongoDB数据修改总结(一)
2014-11-24 07:53:19 来源: 作者: 【 】 浏览:7
Tags:MongoDB 数据 修改 总结
MongoDB数据修改总结
1.前言
最近在学习MongoDB,数据修改这一部分的内容较多,命令比较繁琐,所以将一些常用的修改命令总结在这篇博客中,方便今后学习的查阅。
2.命令总结
1). insert()
db.collection.insert(x) x就是要更新的对象,只能是单条记录,如:
[plain]
db.collection.insert({_id:1,name:"test",count:1})
当需要批量插入的时候,可以在shell中使用for循环,如:
[plain]
for(var i=0;i<16;i++){
db.mytest.insert({_id:i,name:"test"+i,count:i})
}
此时如果用find()命令查询插入的数据,结果是这样的:
[plain]
> db.mytest.find()
{ "_id" : 0, "name" : "test0", "count" : 0 }
{ "_id" : 1, "name" : "test1", "count" : 1 }
{ "_id" : 2, "name" : "test2", "count" : 2 }
{ "_id" : 3, "name" : "test3", "count" : 3 }
{ "_id" : 4, "name" : "test4", "count" : 4 }
{ "_id" : 5, "name" : "test5", "count" : 5 }
{ "_id" : 6, "name" : "test6", "count" : 6 }
{ "_id" : 7, "name" : "test7", "count" : 7 }
{ "_id" : 8, "name" : "test8", "count" : 8 }
{ "_id" : 9, "name" : "test9", "count" : 9 }
{ "_id" : 10, "name" : "test10", "count" : 10 }
{ "_id" : 11, "name" : "test11", "count" : 11 }
{ "_id" : 12, "name" : "test12", "count" : 12 }
{ "_id" : 13, "name" : "test13", "count" : 13 }
{ "_id" : 14, "name" : "test14", "count" : 14 }
{ "_id" : 15, "name" : "test15", "count" : 15 }
2). update()
db.collection.update( criteria, objNew, upsert, multi ) 四个参数的说明如下:
criteria: update的查询条件,类似sql update查询内where后面的
objNew: update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
upsert: 这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi: mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
几个查询例子如下:
db.mytest.update({count:{$gt:1}},{$set:{name:"ok"}}) 只更新第一条记录
db.mytest.update({count:{$gt:3}},{$set:{name:"ok"}},false,true) 大于3的全部更新了
db.mytest.update({count:{$gt:4}},{$set:{name:"ok123"}},true,false) 只更新了一条
db.mytest.update({count:{$gt:6}},{$set:{name:"ok123"}},true,true) 大于6的全部更新了
3). save()
db.collection.save(x) x是要插入的对象,效果与上面的insert命令一样。save与insert的区别是这样的:
在进行插入数据的操作中,当遇到_id相同的情况下,save完成保存操作,insert则会保存;即_id相同情况下,save相当于更新操作。
下面是一些MongoDB的更新操作符
4). $inc
用法:{$inc:{field:value}} 意思是对一个数字字段field增加value:
[plain]
> db.mytest.find({_id:1})
{ "_id" : 1, "name" : "test1", "count" : 1 }
> db.mytest.update({_id:1},{$inc:{count:1}})
> db.mytest.find({_id:1})
{ "_id" : 1, "name" : "test1", "count" : 2 } //count字段加1
value的值也可以为负,就相当于减一个值:
[plain]
> db.mytest.update({_id:1},{$inc:{count:-2}})
> db.mytest.find({_id:1})
{ "_id" : 1, "name" : "test1", "count" : 0 } //值从2减到0
5). $set命令
用法:{$set:{field:value}}
相当于在关系型 数据库中sql的set field=value,全部数据类型都支持$set操作
[plain]
> db.mytest.update({_id:1},{$set:{count:111}})
> db.mytest.find({_id:1})
{ "_id" : 1, "name" : "test1", "count" : 111 } //修改数值型
> db.mytest.update({_id:1},{$set:{name:"MongoDB"}})
> db.mytest.find({_id:1})
{ "_id" : 1, "count" : 111, "name" : "MongoDB" } //修改字符型
6). $unset
用法:{$unset:{field:1}}
[plain]
> db.mytest.find({_id:1})
{ "_id" : 1, "count" : 111, "name" : "MongoDB" }
> db.mytest.update({_id:1},{$unset:{name:1}})
> db.mytest.find({_id:1})
{ "_id" : 1, "count" : 111 } //删除了字段name
7). $push
用法:{$push:{field:value}}
把value追加到field中取,field一定是数据类型才行,如果field不存在,会新增一个数组类型加进去:
[p
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇mongdb文件型数据库开发实例 下一篇数据库核心の增删改查

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C语言中,“指针”用 (2025-12-26 15:20:18)
·在c语言的指针运算中 (2025-12-26 15:20:15)
·C语言-函数指针与函 (2025-12-26 15:20:12)
·求navicat for mysql (2025-12-26 13:21:33)
·有哪位大哥推荐一下m (2025-12-26 13:21:30)