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()

day--86(MongoDB数据库)的更多相关文章

  1. Mongodb数据库学习系列————(一)Mongodb数据库主从复制的搭建

    Mongodb数据库主从复制的搭建 Writeby:lipeng                                    date:2014-10-22 最近项目上用到了位置查询,在网上 ...

  2. 基于C#的MongoDB数据库开发应用(4)--Redis的安装及使用

    在前面介绍了三篇关于MongoDB数据库的开发使用文章,严格来讲这个不能归类于MongoDB数据库开发,不过Redis又有着和MongoDB数据库非常密切的关系,它们两者很接近,Redis主要是内存中 ...

  3. FineReport如何连接和使用MongoDB数据库

    随着NoSQL数据库越来越流行,MongoDB数据库作为NoSQL数据库中的领头羊,使用也越来越广泛.为此,FineReport V8.0版本提供了数据连接和数据集接口,可以通过开发一款可以连接和使用 ...

  4. python操作mongodb数据库

    一.MongoDB 数据库操作 连接数据库 import pymongo conn = pymongo.Connection() # 连接本机数据库 conn = pymongo.Connection ...

  5. NoSql 中Mongodb数据库的使用

    1.NoSql数据库简介 2.MongoDB数据库的简介 3.MongoDB下Windows下的安装

  6. 线上mongodb数据库mLab使用总结

    最近在CNode社区看到有人分享了免费的线上mongodb数据库(容量500M),今天去注册了一下,成功的将线下数据库替换掉了,现在就说一下它的使用和配置需要注意的地方: mLab是一款免费的在线mo ...

  7. mongoDB数据库和Spring MVC的整合

    之前一直用到的项目是Spring MVC+maven+mysql的,最近有些数据需要用到mongoDB数据库,现在做一些总结. 第一步:加载jar.maven配置 <!-- mongodb开始 ...

  8. 【转载】CentOS6.5_X64下安装配置MongoDB数据库

    [转载]CentOS6.5_X64下安装配置MongoDB数据库 2014-05-16 10:07:09|  分类: 默认分类|举报|字号 订阅      下载LOFTER客户端 本文转载自zhm&l ...

  9. 基于C#的MongoDB数据库开发应用(3)--MongoDB数据库的C#开发之异步接口

    在前面的系列博客中,我曾经介绍过,MongoDB数据库的C#驱动已经全面支持异步的处理接口,并且接口的定义几乎是重写了.本篇主要介绍MongoDB数据库的C#驱动的最新接口使用,介绍基于新接口如何实现 ...

  10. 基于C#的MongoDB数据库开发应用(2)--MongoDB数据库的C#开发

    在上篇博客<基于C#的MongoDB数据库开发应用(1)--MongoDB数据库的基础知识和使用>里面,我总结了MongoDB数据库的一些基础信息,并在最后面部分简单介绍了数据库C#驱动的 ...

随机推荐

  1. java基础之集合:List Set Map的概述以及使用场景

    本文的整体思路以及部分文字来源:来源一 和 来源二 Java集合类的基本概念: 首先大家要明白集合为什么会出现: 在编程中,常常需要集中存放多个数据.从传统意义上讲,数组是我们的一个很好的选择,前提是 ...

  2. python子进程模块subprocess详解与应用实例 之一

    subprocess--子进程管理器 一.subprocess 模块简介 subprocess最早是在2.4版本中引入的. subprocess模块用来生成子进程,并可以通过管道连接它们的输入/输出/ ...

  3. uva1619

    分析:这个题的关键是要找到,当某个值是最小值时它最大的影响区间时什么.可以通过单调队列(单调栈)在nlogn的时间内实现 #include <cstdio> #include <cs ...

  4. NOIP2012摆花

    题目描述 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共 m 盆.通过调查顾客的喜好,小明列出了顾客最喜欢的 n 种花,从 1 到 n 标号.为了在门口展出更多种花,规定第 i 种花不 ...

  5. Linux awk&sed

    awk AWK是强大的文本处理工具,擅长对日志文件迚行快速分析. 它丌仅用亍 Linux ,也是任何环境中现有的功能最强大的数据处理引擎之一. 名称得自亍它的发明者 Alfred Aho .Pet ...

  6. box-shadow 内阴影

    1.盒子阴影样式单词:box-shadow 2.语法 代表边框间距靠左0 靠上0 和1px阴影范围 阴影颜色为黑色(#000),有inset 代表框内阴影 ,不带inset 代表框外阴影. 注意:bo ...

  7. 使用RVM更新Ruby 版本

    http://rvm.io/rvm/install Install RVM (development version): \curl -sSL https://get.rvm.io | bash Wi ...

  8. 2-chrome无法添加扩展程序

    1.更多工具->拓展程序->打开开发者模式->重启浏览器 2.将拓展程序拖入,确认安装

  9. 30-懒省事的小明(priority_queue)

    http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=55 懒省事的小明 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   ...

  10. groupie

    def add_group(group):    c = group.c.astype('float')   group['d'] = c/c.sum()   return group df = pd ...