Mongo数据操作
来源:互联网 发布:java实现迭代器 编辑:程序博客网 时间:2024/05/20 20:43
可以根据条件来更新指定的文档,但要注意查询条件的唯一性:
> db.tianyc04.remove()
> db.tianyc04.insert({name:'tyc',age:20})
> db.tianyc04.insert({name:'tyc',age:30})
> db.tianyc04.insert({name:'tyc',age:32})
> db.tianyc04.find()
{ "_id" : ObjectId("510c83ad13d7e50c2281b048"), "name" : "tyc", "age" : 20 }
{ "_id" : ObjectId("510c83b113d7e50c2281b049"), "name" : "tyc", "age" : 30 }
{ "_id" : ObjectId("510c83b313d7e50c2281b04a"), "name" : "tyc", "age" : 32 }
> x=db.tianyc04.findOne()
{ "_id" : ObjectId("510c83ad13d7e50c2281b048"), "name" : "tyc", "age" : 20 }
> x.age++
20
> x
{ "_id" : ObjectId("510c83ad13d7e50c2281b048"), "name" : "tyc", "age" : 21 }
#按照name条件更新,将会更新所有记录。但x中已经包含了_id字段,这样会产生主键重复,所以mongo只会更新第一个。
> db.tianyc04.update({name:'tyc'}, x)
> db.tianyc04.find()
{ "_id" : ObjectId("510c83ad13d7e50c2281b048"), "name" : "tyc", "age" : 21 }
{ "_id" : ObjectId("510c83b113d7e50c2281b049"), "name" : "tyc", "age" : 30 }
{ "_id" : ObjectId("510c83b313d7e50c2281b04a"), "name" : "tyc", "age" : 32 }
#如果我更新第二条数据,会怎么样呢:
> x=db.tianyc04.findOne({age:30})
{ "_id" : ObjectId("510c83b113d7e50c2281b049"), "name" : "tyc", "age" : 30 }
> x.age++
30
> x
{ "_id" : ObjectId("510c83b113d7e50c2281b049"), "name" : "tyc", "age" : 31 }
> db.tianyc04.update({name:'tyc'},x)
> db.tianyc04.find()
{ "_id" : ObjectId("510c83ad13d7e50c2281b048"), "name" : "tyc", "age" : 21 }
{ "_id" : ObjectId("510c83b113d7e50c2281b049"), "name" : "tyc", "age" : 30 }
{ "_id" : ObjectId("510c83b313d7e50c2281b04a"), "name" : "tyc", "age" : 32 }
> db.getLastError()
null
#可以看到第二条数据并没有被更新掉。因为mongo会首先匹配到第一条记录,尝试将其更新为x,但此时会与第二条记录产生主键重复,所以mongo会直接退出,且没有提示错误。
#下面我按照_id来更新第二条记录,这次就更新正常了:
> db.tianyc04.update({_id:ObjectId("510c83b113d7e50c2281b049")},x)
> db.tianyc04.find()
{ "_id" : ObjectId("510c83ad13d7e50c2281b048"), "name" : "tyc", "age" : 21 }
{ "_id" : ObjectId("510c83b113d7e50c2281b049"), "name" : "tyc", "age" : 31 }
{ "_id" : ObjectId("510c83b313d7e50c2281b04a"), "name" : "tyc", "age" : 32 }
>
下面再无聊地使用tcy条件连续更新一下,就看到mongo的更新流程了:
> db.tianyc04.update({name:'tyc'},{name:'new_line'})
> db.tianyc04.find()
{ "_id" : ObjectId("510c83ad13d7e50c2281b048"), "name" : "new_line" }
{ "_id" : ObjectId("510c83b113d7e50c2281b049"), "name" : "tyc", "age" : 31 }
{ "_id" : ObjectId("510c83b313d7e50c2281b04a"), "name" : "tyc", "age" : 32 }
> db.tianyc04.update({name:'tyc'},{xjbu:'bengcaca'})
> db.tianyc04.find()
{ "_id" : ObjectId("510c83ad13d7e50c2281b048"), "name" : "new_line" }
{ "_id" : ObjectId("510c83b113d7e50c2281b049"), "xjbu" : "bengcaca" }
{ "_id" : ObjectId("510c83b313d7e50c2281b04a"), "name" : "tyc", "age" : 32 }
> db.tianyc04.update({name:'tyc'},{tonight:'papapa'})
> db.tianyc04.find()
{ "_id" : ObjectId("510c83ad13d7e50c2281b048"), "name" : "new_line" }
{ "_id" : ObjectId("510c83b113d7e50c2281b049"), "xjbu" : "bengcaca" }
{ "_id" : ObjectId("510c83b313d7e50c2281b04a"), "tonight" : "papapa" }
>
总结:更新时的查询条件建议选择_id。
除了查询条件,还可以使用修改器对文档进行更新。
1. $inc
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "name" : "xtt", "age" : 11 }
> db.tianyc03.update({name:'xtt',age:11},{'$inc':{age:5}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "name" : "xtt", "age" : 16 }
#$inc还可以添加列。
> db.tianyc04.find()
{ "_id" : ObjectId("510c83ad13d7e50c2281b048"), "wc" : 1 }
> db.tianyc04.update({wc:1},{$inc:{score:50}})
> db.tianyc04.find()
{ "_id" : ObjectId("510c83ad13d7e50c2281b048"), "score" : 50, "wc" : 1 }
>
2. $set
用来指定一个键的值,若该键不存在,则创建。如果需要删除该键,使用$unset。
#新增加列sex
> db.tianyc03.update({name:'xtt',age:16},{$set:{sex:'m'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "name" : "xtt", "sex" : "m" }
#修改列sex
> db.tianyc03.update({name:'xtt',age:16},{$set:{sex:'male'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "name" : "xtt", "sex" : "male" }
#改变列sex数据类型
> db.tianyc03.update({name:'xtt',age:16},{$set:{sex:1}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "name" : "xtt", "sex" : 1 }
#删除列sex
> db.tianyc03.update({name:'xtt',age:16},{$unset:{sex:1}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "name" : "xtt" }
3. $push
用来增加数组。若没有改列,则会自动增加。
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "name" : "xtt" }
> db.tianyc03.update({'name':'xtt'},{$push:{companies:'neu'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu" ], "name" : "xtt" }
> db.tianyc03.update({'name':'xtt'},{$push:{companies:'alibaba'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu", "alibaba" ], "name" : "xtt" }
4. $addToSet
#push操作不会检查数组中元素是否重复,如果需要将不重复的数据加入数组,需要使用$addToSet
> db.tianyc03.update({'name':'xtt'},{$addToSet:{companies:'alibaba'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu", "alibaba", "alibaba" ], "name" : "xtt" }
> db.tianyc03.update({'name':'xtt'},{$addToSet:{companies:'congxing'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu", "alibaba", "alibaba", "congxing" ], "name" : "xtt" }
> db.tianyc03.update({'name':'xtt'},{$addToSet:{companies:'congxing'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu", "alibaba", "alibaba", "congxing" ], "name" : "xtt" }
>
5. $pop
通过$pop可以从数组中移除数据。此时数据被看成是一个队列。使用{$pop:{key:1}}将从队列末尾移除元素,使用{$pop:{key:-1}}将从队列开头移除元素。
> db.tianyc03.update({'name':'xtt'},{$addToSet:{companies:['c1','c2']}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu", "alibaba", "alibaba", "congxing", [ "c1", "c2" ] ], "name" : "xtt" }
> db.tianyc03.update({'name':'xtt'},{$pop:{companies:1}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu", "alibaba", "alibaba", "congxing" ], "name" : "xtt" }
>
> db.tianyc03.update({'name':'xtt'},{$pop:{companies:-1}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "alibaba", "alibaba", "congxing" ], "name" : "xtt" }
>
6. $pull
除了 $pop,也可以使用 $pull 来删除数组中的元素,此时可以根据元素值进行匹配,将匹配上的元素全部删除。
> db.tianyc03.update({'name':'xtt'},{$addToSet:{companies:'teletek'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "alibaba", "alibaba", "congxing", "teletek" ], "name" : "xtt" }
> db.tianyc03.update({},{$pull:{companies:'congxing'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "alibaba", "alibaba", "teletek" ], "name" : "xtt" }
> db.tianyc03.update({},{$pull:{companies:'alibaba'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "teletek" ], "name" : "xtt" }
>
- Mongo数据操作
- mongo 的crud操作&&mongo的数据聚合
- mongo CRUD数据操作符汇总
- mongo操作
- Mongo操作
- mongo操作
- 自制搜索(elasticsearch安装,mongo-connector同步数据,python操作)
- mongo 使用 in操作
- mongo常用操作
- mongo基本操作
- Java 操作Mongo
- mongo-基本操作
- mongo 高级操作
- Mongo 操作命令
- mongo的常用操作
- mongo的常用操作
- nodejs+mongoskin操作mongo
- Mongo基本操作
- TCP协议的流量控制、拥塞控制和差错控制
- wiki-1040统计单词个数
- 学习网站整理
- VC MFC 串口通信(多线程)
- 关于字符串的内存分配问题
- Mongo数据操作
- 如果需要详细题解的进来
- AppStore应用发布流程
- jQuery Ajax请求-jQuery.getScript(url, [callback])
- 解决 Win7 64位安装Oracle11g时出现的 [INS-20802] Oracle Net Configuration Assistant failed 错误
- C#计算时间间隔的方法小结
- Ubuntu的shell之bash和dash
- mysql集群配置
- 线程同步互斥的控制方法 互斥信号量区别