mongodb数据库基本操作指令

ps:mongodb中的 文档,集合的概念(和mysql中的表对比理解):

集合(mongodb)--相当于mysql中的表
文档(mongodb)--相当于mysql中的行

一:mongodb数据库操作

  use blog  #切换到这个数据库,如果没有则创建,有则切换
switched to db blog
> show dbs #显示所有的数据库
admin 0.000GB
blog 0.000GB
config 0.000GB
local 0.000GB
> db.article.insert({"title":"python"}) #db代表当前切换的数据库blog
WriteResult({ "nInserted" : 1 })
> db.userinfo.insert({"name":"tom"}) #往数据库添加集合(mysql中叫做表,mongodb中叫做 集合)
WriteResult({ "nInserted" : 1 })
> show tables; #显示所有的集合
article
userinfo
> show dbs
admin 0.000GB
blog 0.000GB
config 0.000GB
local 0.000GB > db.dropDatabase() #删除这个数据库
{ "dropped" : "blog", "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB

二:mongodb数据库的集合操作(mysql中称之为:表)

  > use blog
switched to db blog
> db.article.insert({"name":"c++"})
WriteResult({ "nInserted" : 1 })
> db.article.insert({"name":"linux"})
WriteResult({ "nInserted" : 1 })
> show tables; #显示当前所有的集合
article
> db.article.drop() #删除这个文档
true #显示已经删除
> show tables #再显示,已经没有了,所以没有打印出来

三:文档操作(相当于mysql中的行)

  1:添加文档

01:没有指定_id则默认ObjectId,_id不能重复,且在插入后不可变
:插入单条文档
cmd界面操作:
小技巧: ^ 放在cmd界面一行的前面和最后面,用来换行,因此才会,在文档结束的时 候,再加上^就会 结束换行
> user0={^
... "name":"tom",
... "age":10,
... "hobbies":["musice","read","dancing"],
... "addr":{"countye":"China","city":"BJ"}
... ^}
2019-03-05T20:36:20.263+0800 E QUERY [js] Synt
> db.test.insert(user0)
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("5c7e6d52253ec476879e656a") }
:插入多条
user1={
"_id":1,
"name":"alex",
"age":10,
'hobbies':['music','read','dancing'],
'addr':{
'country':'China',
'city':'weifang'
}
} user2={
"_id":2,
"name":"wupeiqi",
"age":20,
'hobbies':['music','read','run'],
'addr':{
'country':'China',
'city':'hebei'
}
} user3={
"_id":3,
"name":"yuanhao",
"age":30,
'hobbies':['music','drink'],
'addr':{
'country':'China',
'city':'heibei'
}
} user4={
"_id":4,
"name":"jingliyang",
"age":40,
'hobbies':['music','read','dancing','tea'],
'addr':{
'country':'China',
'city':'BJ'
}
} user5={
"_id":5,
"name":"jinxin",
"age":50,
'hobbies':['music','read',],
'addr':{
'country':'China',
'city':'henan'
}
} >db.user.insertMany([user1,user2,user3,user4,user5]) > db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 10, "hobbies" : [ "music", "read", "dancing" ], "addr" : { "country" : "China", "city" : "weifang" } }
{ "_id" : 2, "name" : "wupeiqi", "age" : 20, "hobbies" : [ "music", "read", "run" ], "addr" : { "country" : "China", "city" : "hebei" } }
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 50, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
>

2:查看文档

  001:比较运算
  

# SQL:=,!=,>,<,>=,<=
# MongoDB:{key:value}代表什么等于什么,"$ne","$gt","$lt","gte","lte",
其中"$ne"能用于所有数据类型 #1、select * from db1.user where name = "alex"; #(mysql)
> db.user.find({"name":"alex"})
{ "_id" : 1, "name" : "alex", "age" : 10, "hobbies" : [ "music", "read", "dancing" ], "addr" : { "country" : "China", "city" : "weifang" } } #2、select * from db1.user where name != "alex"; #(mysql)
> db.user.find({"name":{"$ne":"alex"}})
{ "_id" : 2, "name" : "wupeiqi", "age" : 20, "hobbies" : [ "music", "read", "run" ], "addr" : { "country" : "China", "city" : "hebei" } }
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 50, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } } #3、select * from db1.user where id > 2; #(mysql)
> db.user.find({'_id':{'$gt':2}})
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 50, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
> #4、select * from db1.user where id < 3; #(mysql)
> db.user.find({'_id':{"$gte":2,}})
{ "_id" : 2, "name" : "wupeiqi", "age" : 20, "hobbies" : [ "music", "read", "run" ], "addr" : { "country" : "China", "city" : "hebei" } }
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 50, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
>
#5、select * from db1.user where id <= 2; #(mysql)
> db.user.find({"_id":{"$lte":2}})
{ "_id" : 1, "name" : "alex", "age" : 10, "hobbies" : [ "music", "read", "dancing" ], "addr" : { "country" : "China", "city" : "weifang" } }
{ "_id" : 2, "name" : "wupeiqi", "age" : 20, "hobbies" : [ "music", "read", "run" ], "addr" : { "country" : "China", "city" : "hebei" } }
>
  002:逻辑运算
  

# MySQL:and,or,not
# MongoDB:字典中逗号分隔的多个条件是and关系,"$or"的条件放到[]内,"$not"
----------------理解小概念-------------
ps:db:表示当前的数据库,user表示当前数据库下的这个集合(表) #1、select * from db1.user where id >= 2 and id < 4;
> db.user.find({"_id":{"$gt":2,"$lt":4}}) # 意思是:找到当前数据库下的user集合下的数据
结果:{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
> #2、select * from db1.user where id >= 2 and age < 40;
> db.user.find({"_id":{"$gte":2},"age":{"$lt":40}})
结果: { "_id" : 2, "name" : "wupeiqi", "age" : 20, "hobbies" : [ "music", "read", "run" ], "addr" : { "country" : "China", "city" : "hebei" } }
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
> #3、select * from db1.user where id >= 5 or name = "alex";
db.user.find({
"$or":[
{'_id':{"$gte":5}},
{"name":"alex"}
]
}) 结果: >{ "_id" : 1, "name" : "alex", "age" : 10, "hobbies" : [ "music", "read", "dancing" ], "addr" : { "country" : "China", "city" : "weifang" } }
{ "_id" : 5, "name" : "jinxin", "age" : 50, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } } #4、select * from db1.user where id >= 5 or name = "alex";
> db.user.find({"_id":{"$mod":[2,1]}}) #将id的值除2取1,(取奇数)
结果:{ "_id" : 1, "name" : "alex", "age" : 10, "hobbies" : [ "music", "read", "dancing" ], "addr" : { "country" : "China", "city" : "weifang" } }
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 5, "name" : "jinxin", "age" : 50, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
>
#5、取反:
db.user.find({'_id':{"$not":{"$mod":[2,1]}}}) #拿到所有不是奇数的数据
003:成员运算
# mysql: in,not in
#mongodb: "$in","$nin" #1、select * from db1.user where age in (20,30,31);
> db.user.find({"age":{"$in":[20,30,31]}}) #取出年龄在这个集合之中的数据
{ "_id" : 2, "name" : "wupeiqi", "age" : 20, "hobbies" : [ "music", "read", "run" ], "addr" : { "country" : "China", "city" : "hebei" } }
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
> #2、select * from db1.user where name not in ('alex','yuanhao');
> db.user.find({"name":{"$nin":["alex","yuanhao"]}}) #取出姓名不在这个集合中的数据
{ "_id" : 2, "name" : "wupeiqi", "age" : 20, "hobbies" : [ "music", "read", "run" ], "addr" : { "country" : "China", "city" : "hebei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" }}
{ "_id" : 5, "name" : "jinxin", "age" : 50, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
>
004:正则匹配
# SQL: regexp 正则
# MongoDB: /正则表达/i
#1、select * from db1.user where name regexp '^j.*?(g|n)$';
> db.user.find({'name':/^j.*?(g|n)$/i})
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 50, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
>
  005:取指定字段
#1、select name,age from db1.user where id=3;
> db.user.find({"_id":3},{'name':1,"age":1}) #取id为3的数据,并且显示姓名和年龄,1 代表显示,0 代表不显示
{ "_id" : 3, "name" : "yuanhao", "age" : 30 }
> #2:查询出id为3的数据,并且不显示地址,其他字段默认显示,
> db.user.find({"_id":3},{"addr":0})
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ] }
>
006:查询数组
#1、查看有dancing爱好的人
> db.user.find({"hobbies":"dancing"})
-->
{ "_id" : 1, "name" : "alex", "age" : 10, "hobbies" : [ "music", "read", "dancing" ], "addr" : { "country" : "China", "city" : "weifang" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
> #2、查看既有dancing爱好又有tea爱好的人
db.user.find({
'hobbies':{
"$all":['dancing','tea']
}
})
-->
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } } #3、查看第4个爱好为tea的人
db.user.find({"hobbies.3":'tea'})
-->
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } } #4、查看所有人最后两个爱好
db.user.find({},{'hobbies':{"$slice":-2},"age":0,"_id":0,"name":0,"addr":0}) #0代表字段不显示
-->
{ "hobbies" : [ "read", "dancing" ] }
{ "hobbies" : [ "read", "run" ] }
{ "hobbies" : [ "music", "drink" ] }
{ "hobbies" : [ "dancing", "tea" ] }
{ "hobbies" : [ "music", "read" ] } #5、查看所有人的第2个到第3个爱好
db.user.find({},{'hobbies':{"$slice":[1,2]},"age":0,"_id":0,"name":0,"addr":0})
--> #这个是时候的切片,两边都是闭区间。----〉取头,取尾
{ "hobbies" : [ "read", "dancing" ] }
{ "hobbies" : [ "read", "run" ] }
{ "hobbies" : [ "drink" ] }
{ "hobbies" : [ "read", "dancing" ] }
{ "hobbies" : [ "read" ] }
  007:排序
# 排序: 1代表升序,-1代表降序
db.user.find().sort({"name":1,}) #按名字的升序排列
--〉
> db.user.find().sort({"name":1,})
{ "_id" : 1, "name" : "alex", "age" : 10, "hobbies" : [ "music", "read", "dancing" ], "addr" : { "country" : "China", "city" : "weifang" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 50, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
{ "_id" : 2, "name" : "wupeiqi", "age" : 20, "hobbies" : [ "music", "read", "run" ], "addr" : { "country" : "China", "city" : "hebei" } }
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
> db.user.find().sort({"age":-1,'_id':1}) # 按照年龄降序排列,如果年龄一样,则按照id 升序排列
--〉
"_id" : 5, "name" : "jinxin", "age" : 50, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
"_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
"_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
"_id" : 2, "name" : "wupeiqi", "age" : 20, "hobbies" : [ "music", "read", "run" ], "addr" : { "country" : "China", "city" : "hebei" } }
"_id" : 1, "name" : "alex", "age" : 10, "hobbies" : [ "music", "read", "dancing" ], "addr" : { "country" : "China", "city" : "weifang" } }
008:分页(limit代表取多少个document,skip代表跳过前多少个document。)
db.user.find().sort({'age':1}).limit(1).skip(2)
-->
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
  009:查询数量
第一种方法:
> db.user.count({"age":{"$gt":30}})
---> 2 第二种方法:
> db.user.find({"age":{"$gt":30}}).count()
--->2
  010:其他方法:
#1、{'key':null} 匹配key的值为null或者没有这个key
> db.t2.insert({"a":10,"b":111})
WriteResult({ "nInserted" : 1 })
> db.t2.insert({"a":20})
WriteResult({ "nInserted" : 1 })
> db.t2.insert({"b":null})
WriteResult({ "nInserted" : 1 })
>
> db.t2.find({"b":null})
{ "_id" : ObjectId("5c7f23577e39bd0c0f8fd7ba"), "a" : 20 }
{ "_id" : ObjectId("5c7f236a7e39bd0c0f8fd7bb"), "b" : null }
#2、查找所有
db.user.find() #等同于db.user.find({})
db.user.find().pretty() #pretty() 格式化输出,使结果在cmd界面是换行输出的 #3、查找一个,与find用法一致,只是只取匹配成功的第一个
db.user.findOne() #取所有结果的第一个且格式化输出
db.user.findOne({"_id":{"$gt":3}})  #取所有结果的第一个且格式化输出

3:修改文档update

update() 方法用于更新已存在的文档。语法格式如下:

db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
) 参数说明:对比update db1.t1 set name='EGON',sex='Male' where name='egon' and age=18; query : 相当于where条件。
update : update的对象和一些更新的操作符(如$,$inc...等,相当于set后面的
upsert : 可选,默认为false,代表如果不存在update的记录不更新也不插入,设置为true代表插入。
multi : 可选,默认为false,代表只更新找到的第一条记录,设为true,代表更新找到的全部记录。
writeConcern :可选,抛出异常的级别。 更新操作是不可分割的:若两个更新同时发送,先到达服务器的先执行,然后执行另外一个,不会破坏文档。
  001:覆盖更新  
第一种方法:
原来的数据:
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 10, "hobbies" : [ "music", "read", "dancing" ], "addr" : { "country" : "China", "city" : "weifang" } }
{ "_id" : 2, "name" : "wupeiqi", "age" : 20, "hobbies" : [ "music", "read", "run" ], "addr" : { "country" : "China", "city" : "hebei" } }
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 50, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } } 更新后的数据:
#查询年龄为20 的数据,并将这条数据替换为后面的数据,默认是替换找到的第一条数据,也可以设置multi=True,则替换所有找到的数据
> db.user.update({"age":20},{"name":"Wxx","hobbies_count":3})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 10, "hobbies" : [ "music", "read", "dancing" ], "addr" : { "country" : "China", "city" : "weifang" } }
{ "_id" : 2, "name" : "Wxx", "hobbies_count" : 3 }
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 50, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
>
说明:是用{"_id":2,"name":"Wxx","hobbies_count":3}覆盖原来的记录原来的记录就不会存在了 第二种方法:
002:一种最简单的更新就是用一个新的文档完全替换匹配的文档。这适用于大规模式迁移的情况。例如
var obj=db.user.findOne({"_id":2}) obj.username=obj.name+'SB'
obj.hobbies_count++
delete obj.age db.user.update({"_id":2},obj)
  002:局部更新
通常文档只会有一部分需要更新。可以使用原子性的更新修改器,指定对文档中的某些字段进行更新。更新修改器是种特殊的键,用来指定复杂的更新操作,
比如修改、增加后者删除设置:$set
#1:#1、update db1.user set  name="WXX" where id = 2
> db.user.update({"_id":2},{"$set":{"name":"MMM",}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 10, "hobbies" : [ "music", "read", "dancing" ], "addr" : { "country" : "China", "city" : "weifang" } }
{ "_id" : 2, "name" : "MMM", "hobbies_count" : 3 }
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 50, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } } #2、没有匹配成功则新增一条{"upsert":true} #--------
upsert : 可选,默认为false,代表如果不存在update的记录不更新也不插入,设置为true代表插入。 db.user.update({"_id":6},{"$set":{"name":"tom","age":18}},{"upsert":true})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 6 })
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 10, "hobbies" : [ "music", "read", "dancing" ], "addr" : { "country" : "China", "city" : "weifang" } }
{ "_id" : 2, "name" : "MMM", "hobbies_count" : 3 }
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 50, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
{ "_id" : 6, "age" : 18, "name" : "tom" }
> #3、默认只改匹配成功的第一条,{"multi":改多条}
db.user.update({'_id':{"$gt":4}},{"$set":{"age":28}})
-->默认第一条
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 10, "hobbies" : [ "music", "read", "dancing" ], "addr" : { "country" : "China", "city" : "weifang" } }
{ "_id" : 2, "name" : "MMM", "hobbies_count" : 4, "username" : "MMMSB" }
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 28, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
{ "_id" : 6, "age" : 18, "name" : "tom" } -->multi设置可以改多条
将id大于4的文档的年龄 改为 38 ,全部匹配的都改
> db.user.update({'_id':{"$gt":4}},{"$set":{"age":38}},{"multi":true}) # true 必须小写
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 10, "hobbies" : [ "music", "read", "dancing" ], "addr" : { "country" : "China", "city" : "weifang" } }
{ "_id" : 2, "name" : "MMM", "hobbies_count" : 4, "username" : "MMMSB" }
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 38, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
{ "_id" : 6, "age" : 38, "name" : "tom" } 4、修改内嵌文档,把名字为alex的人所在的地址国家改成Japan
> db.user.update({"name":"alex"},{"$set":{"addr.country":"Japan"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 10, "hobbies" : [ "music", "read", "dancing" ], "addr" : { "country" : "Japan", "city" : "weifang" } }
{ "_id" : 2, "name" : "MMM", "hobbies_count" : 4, "username" : "MMMSB" }
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 38, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
{ "_id" : 6, "age" : 38, "name" : "tom" } #5、把名字为alex的人的第2个爱好改成eat
> db.user.update({"name":"alex"},{"$set":{"hobbies.1":"eat"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 10, "hobbies" : [ "music", "eat", "dancing" ], "addr" : { "country" : "Japan", "city" : "weifang" } }
{ "_id" : 2, "name" : "MMM", "hobbies_count" : 4, "username" : "MMMSB" }
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 38, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
{ "_id" : 6, "age" : 38, "name" : "tom" }
>
#6、删除alex的爱好,$unset
> db.user.update({"name":"axle"},{"$unset":{"hobbies":""}})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 10, "hobbies" : [ "music", "eat", "dancing" ], "addr" : { "country" : "Japan", "city" : "weifang" } }
{ "_id" : 2, "name" : "MMM", "hobbies_count" : 4, "username" : "MMMSB" }
{ "_id" : 3, "name" : "yuanhao", "age" : 30, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 40, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 38, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
{ "_id" : 6, "age" : 38, "name" : "tom" }
  003:增加和减少
#1、所有人年龄增加一岁
> db.user.update({},{"$inc":{"age":1}},{"multi":true})
WriteResult({ "nMatched" : 6, "nUpserted" : 0, "nModified" : 6 })
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 11, "hobbies" : [ "music", "eat", "dancing" ], "addr" : { "country" : "Japan", "city" : "weifang" } }
{ "_id" : 2, "name" : "MMM", "hobbies_count" : 4, "username" : "MMMSB", "age" : 1 }
{ "_id" : 3, "name" : "yuanhao", "age" : 31, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 41, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 39, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
{ "_id" : 6, "age" : 39, "name" : "tom" } #2、所有人年龄减少5岁
> db.user.update({},{"$inc":{"age":-5}},{"multi":true})
WriteResult({ "nMatched" : 6, "nUpserted" : 0, "nModified" : 6 })
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 6, "hobbies" : [ "music", "eat", "dancing" ], "addr" : { "country" : "Japan", "city" : "weifang" } }
{ "_id" : 2, "name" : "MMM", "hobbies_count" : 4, "username" : "MMMSB", "age" : -4 }
{ "_id" : 3, "name" : "yuanhao", "age" : 26, "hobbies" : [ "music", "drink" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 36, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 34, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
{ "_id" : 6, "age" : 34, "name" : "tom" }

4:添加删除数组内元素

  #添加删除数组内元素:$push,$pop,$pull往数组内添加元素:$push
#1、为名字为yuanhao的人添加一个爱好read
> db.user.update({"name":"yuanhao"},{"$push":{"hobbies":"read"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 6, "hobbies" : [ "music", "eat", "dancing" ], "addr" : { "country" : "Japan", "city" : "weifang" } }
{ "_id" : 2, "name" : "MMM", "hobbies_count" : 4, "username" : "MMMSB", "age" : -4 }
{ "_id" : 3, "name" : "yuanhao", "age" : 26, "hobbies" : [ "music", "drink", "tea", "dancing", "read" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 36, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 34, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
{ "_id" : 6, "age" : 34, "name" : "tom" }
>
--------------------------------------$each-------------------------------------------------------------
#2、为名字为yuanhao的人一次添加多个爱好tea,dancing
> db.user.update({"name":"yuanhao"},{"$push":{"hobbies":{"$each":["tea","dancing"]}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 6, "hobbies" : [ "music", "eat", "dancing" ], "addr" : { "country" : "Japan", "city" : "weifang" } }
{ "_id" : 2, "name" : "MMM", "hobbies_count" : 4, "username" : "MMMSB", "age" : -4 }
{ "_id" : 3, "name" : "yuanhao", "age" : 26, "hobbies" : [ "music", "drink", "tea", "dancing" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 36, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 34, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
{ "_id" : 6, "age" : 34, "name" : "tom" } 按照位置且只能从开头或结尾删除元素:$pop
#3、{"$pop":{"key":1}} 从数组末尾删除一个元素 > db.user.update({"name":"yuanhao"},{"$pop":{"hobbies":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 6, "hobbies" : [ "music", "eat", "dancing" ], "addr" : { "country" : "Japan", "city" : "weifang" } }
{ "_id" : 2, "name" : "MMM", "hobbies_count" : 4, "username" : "MMMSB", "age" : -4 }
{ "_id" : 3, "name" : "yuanhao", "age" : 26, "hobbies" : [ "music", "drink", "tea", "dancing" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 36, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 34, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
{ "_id" : 6, "age" : 34, "name" : "tom" }
>
#4、{"$pop":{"key":-1}} 从头部删除 > db.user.update({"name":"yuanhao"},{"$pop":{"hobbies":-1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 6, "hobbies" : [ "music", "eat", "dancing" ], "addr" : { "country" : "Japan", "city" : "weifang" } }
{ "_id" : 2, "name" : "MMM", "hobbies_count" : 4, "username" : "MMMSB", "age" : -4 }
{ "_id" : 3, "name" : "yuanhao", "age" : 26, "hobbies" : [ "drink", "tea", "dancing" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 36, "hobbies" : [ "music", "read", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 34, "hobbies" : [ "music", "read" ], "addr" : { "country" : "China", "city" : "henan" } }
{ "_id" : 6, "age" : 34, "name" : "tom" }
>
#5、按照条件删除元素,:"$pull" 把符合条件的统统删掉,而$pop只能从两端删
> db.user.update({"addr.country":"China"},{"$pull":{"hobbies":"read"}},{"multi":true})
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 2 })
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 6, "hobbies" : [ "music", "eat", "dancing" ], "addr" : { "country" : "Japan", "city" : "weifang" } }
{ "_id" : 2, "name" : "MMM", "hobbies_count" : 4, "username" : "MMMSB", "age" : -4 }
{ "_id" : 3, "name" : "yuanhao", "age" : 26, "hobbies" : [ "drink", "tea", "dancing" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 36, "hobbies" : [ "music", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 34, "hobbies" : [ "music" ], "addr" : { "country" : "China", "city" : "henan" } }
{ "_id" : 6, "age" : 34, "name" : "tom" }

5:避免重复添加

#避免添加重复:"$addToSet"

db.urls.insert({"_id":1,"urls":[]})  #第一条文档

db.urls.update({"_id":1},{"$addToSet":{"urls":'http://www.baidu.com'}}) #第二条文档
db.urls.update({"_id":1},{"$addToSet":{"urls":'http://www.baidu.com'}}) #第三条文档
db.urls.update({"_id":1},{"$addToSet":{"urls":'http://www.baidu.com'}}) #第四条文档 #第五条文档
db.urls.update({"_id":1},{
"$addToSet":{
"urls":{
"$each":[
'http://www.baidu.com',
'http://www.baidu.com',
'http://www.xxxx.com'
]
}
}
}
) 查看urls集合:
> db.urls.find()
{ "_id" : 1, "urls" : { } } 总结:"$addToSet" 在集合中有记录的时候,由于id是唯一的,所以后面的数据都没有添加进去

6:其他:

#1、限制大小"$slice",只留最后n个

    db.user.update({"_id":5},{
"$push":{"hobbies":{
"$each":["read",'music','dancing'], #先往条文档添加三个hobby,再只取最后两个hobby
"$slice":-2 # 只保留最后两个hobbies
}
}
}) 结果是:
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 6, "hobbies" : [ "music", "eat", "dancing" ], "addr" : { "country" : "Japan", "city" : "weifang" } }
{ "_id" : 2, "name" : "MMM", "hobbies_count" : 4, "username" : "MMMSB", "age" : -4 }
{ "_id" : 3, "name" : "yuanhao", "age" : 26, "hobbies" : [ "drink", "tea", "dancing" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 36, "hobbies" : [ "music", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 34, "hobbies" : [ "music", "dancing" ], "addr" : { "country" : "China", "city" : "henan" } }
{ "_id" : 6, "age" : 34, "name" : "tom" } #2、排序The $sort element value must be either 1 or -1" db.user.update({"_id":5},{
"$push":{"hobbies":{
"$each":["read",'music','dancing'], # $push 往这个条文档里面添加三个hobbies,
"$slice":-1, # 取倒数第一个(最尾部的第一个)
"$sort":-1 #排序:倒叙
}
}
}) 结果:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 6, "hobbies" : [ "music", "eat", "dancing" ], "addr" : { "country" : "Japan", "city" : "weifang" } }
{ "_id" : 2, "name" : "MMM", "hobbies_count" : 4, "username" : "MMMSB", "age" : -4 }
{ "_id" : 3, "name" : "yuanhao", "age" : 26, "hobbies" : [ "drink", "tea", "dancing" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 36, "hobbies" : [ "music", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 34, "hobbies" : [ "dancing" ], "addr" : { "country" : "China", "city" : "henan" } }
{ "_id" : 6, "age" : 34, "name" : "tom" }
> #注意:不能只将"$slice"或者"$sort"与"$push"配合使用,且必须使用"$each"

4:删除文档

#1、删除多个中的第一个
db.user.deleteOne({ 'age': 8 })
原来的数据:
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 6, "hobbies" : [ "music", "eat", "dancing" ], "addr" : { "country" : "Japan", "city" : "weifang" } }
{ "_id" : 2, "name" : "MMM", "hobbies_count" : 4, "username" : "MMMSB", "age" : -4 }
{ "_id" : 3, "name" : "yuanhao", "age" : 26, "hobbies" : [ "drink", "tea", "dancing" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 36, "hobbies" : [ "music", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 5, "name" : "jinxin", "age" : 34, "hobbies" : [ "dancing" ], "addr" : { "country" : "China", "city" : "henan" } }
{ "_id" : 6, "age" : 34, "name" : "tom" }
> 删除后的数据 #只删除了多个中的第一条匹配数据
> db.user.deleteOne({"age":34})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.user.find()
{ "_id" : 1, "name" : "alex", "age" : 6, "hobbies" : [ "music", "eat", "dancing" ], "addr" : { "country" : "Japan", "city" : "weifang" } }
{ "_id" : 2, "name" : "MMM", "hobbies_count" : 4, "username" : "MMMSB", "age" : -4 }
{ "_id" : 3, "name" : "yuanhao", "age" : 26, "hobbies" : [ "drink", "tea", "dancing" ], "addr" : { "country" : "China", "city" : "heibei" } }
{ "_id" : 4, "name" : "jingliyang", "age" : 36, "hobbies" : [ "music", "dancing", "tea" ], "addr" : { "country" : "China", "city" : "BJ" } }
{ "_id" : 6, "age" : 34, "name" : "tom" }
> #3、删除全部
> db.user.deleteMany({})
{ "acknowledged" : true, "deletedCount" : 5 } #显示删除成功!
> db.user.find()
05-11 14:07