- A+
目录
5.2 find()/findOne()指定返回的fileds
1. 登录/查看/进入数据库
1.登录数据库
mongo --port 数据库端口号
2.查看所有数据库
shard1:PRIMARY> show dbs admin 0.000GB local 0.016GB
3.进入指定数据库
shard1:PRIMARY> use local switched to db local
2. 创建数据库
创建zyl 数据库
#进入我们想创建的数据库 shard1:PRIMARY> use zyl switched to db zyl #查看当前zyl数据库并没有创建 shard1:PRIMARY> show dbs admin 0.000GB local 0.016GB #在当前zyl数据库中插入数据创建集合users shard1:PRIMARY> db.users.insert({name:"jack",sex:1,age:33}) WriteResult({ "nInserted" : 1 }) #再次查看当前的数据库 shard1:PRIMARY> show dbs admin 0.000GB local 0.016GB zyl 0.000GB
说明:MongoDB没有直接创建数据库的命令,是通过先进入数据库再插入数据库才会生成数据库
3. 查看当前数据库中集合
shard1:PRIMARY> show collections users
4. 插入数据/创建集合
1. 插入以下4条数据
第一种方法:
db.users1.insert({name:"jack",sex:1,age:33}) db.users1.insert({name:"lily",sex:0,age:13}) db.users1.insert({name:"kaily",sex:0,age:33}) db.users1.insert({name:"tom",sex:1,age:53})
第二种方法:
db.users1.save({name:"jack",sex:1,age:33}) db.users1.save({name:"lily",sex:0,age:13}) db.users1.save({name:"kaily",sex:0,age:33}) db.users1.save({name:"tom",sex:1,age:53})
2. 插入后查看集合 users1 是否创建成功
shard1:PRIMARY> show collections users users1
3. 查看集合uses1所有数据
shard1:PRIMARY> db.users1.find() { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f2"), "name" : "jack", "sex" : 1, "age" : 33 } { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f3"), "name" : "lily", "sex" : 0, "age" : 13 } { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f4"), "name" : "kaily", "sex" : 0, "age" : 33 } { "_id" : ObjectId("591130d79b8c9f6e4cd1c8f5"), "name" : "tom", "sex" : 1, "age" : 53 }
5. 查询数据
MongoDB使用find来进行查询,查询就是返回一个集合中文档的子集,子集合的范围从0个文档到整个集合。find的第一个参数决定了要返回那些文档(document的过滤条件),其形式也是一个文档,说明要查询的细节,空的查询文档{}会匹配集合的全部内容,要是不指定查询文档,默认是{},如db.users.find()返回集合中的所有内容。向查询文档中添加键值对,就意味着添加了查询条件,对绝大多试类型来说,整数匹配整数,布尔类型匹配布尔类型,字符串匹配字符串。
5.1 find()/findOne()条件过滤
通过增加条件查询到我们想要的内容
#单条件查询 shard1:PRIMARY> db.users1.find({name:"jack"}) { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f2"), "name" : "jack", "sex" : 1, "age" : 33 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f6"), "name" : "jack", "sex" : 1, "age" : 33 }
只获取查询到的第一条文档
shard1:PRIMARY> db.users1.findOne({name:"jack"}) { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f2"), "name" : "jack", "sex" : 1, "age" : 33 }
5.2 find()/findOne()指定返回的fileds
find()的第二个参数限制返回的filed的个数,0代表不返回,1代表返回。"_id"键总是会被返回。如果不带条件,只限制返回的filed个数的话,命令如下:db.users1.find({},{sex:0})。只需要第一个参数为{}空字典就可以。
只获取name等于jack的users1,并且filed为name,age的数据。
shard1:PRIMARY> db.users1.find({name:"jack"},{name:1,age:1}) { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f2"), "name" : "jack", "age" : 33 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f6"), "name" : "jack", "age" : 33 } shard1:PRIMARY> db.users1.find({name:"jack"},{sex:1}) { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f2"), "sex" : 1 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f6"), "sex" : 1 } shard1:PRIMARY> db.users1.findOne({name:"jack"},{sex:1}) { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f2"), "sex" : 1 }
5.3 查询条件
查询条件 |
含义 |
范例 |
$gt |
大于 |
{age:{$gt:33}} |
$gte |
大于等于 |
{age:{$gte:33}} |
$lt |
小于 |
{age:{$lt:33}} |
$lte |
小于等于 |
{age:{$lte:33}} |
$ne |
不等于 |
{age:{$ne:33}} |
$in |
多条件 |
{age:{$in:[13,53]}} |
$or |
或 |
{or:[{age:{in:[13,53]}},{name:"kaily"}]} |
1.大于
shard1:PRIMARY> db.users1.find({age:{$gt:33}}) { "_id" : ObjectId("591130d79b8c9f6e4cd1c8f5"), "name" : "tom", "sex" : 1, "age" : 53 } { "_id" : ObjectId("591132899b8c9f6e4cd1c8f9"), "name" : "tom", "sex" : 1, "age" : 53 }
2.大于等于
shard1:PRIMARY> db.users1.find({age:{$gte:33}}) { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f2"), "name" : "jack", "sex" : 1, "age" : 33 } { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f4"), "name" : "kaily", "sex" : 0, "age" : 33 } { "_id" : ObjectId("591130d79b8c9f6e4cd1c8f5"), "name" : "tom", "sex" : 1, "age" : 53 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f6"), "name" : "jack", "sex" : 1, "age" : 33 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f8"), "name" : "kaily", "sex" : 0, "age" : 33 } { "_id" : ObjectId("591132899b8c9f6e4cd1c8f9"), "name" : "tom", "sex" : 1, "age" : 53 }
3.小于
shard1:PRIMARY> db.users1.find({age:{$lt:33}}) { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f3"), "name" : "lily", "sex" : 0, "age" : 13 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f7"), "name" : "lily", "sex" : 0, "age" : 13 }
4.小于等于
shard1:PRIMARY> db.users1.find({age:{$lte:33}}) { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f2"), "name" : "jack", "sex" : 1, "age" : 33 } { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f3"), "name" : "lily", "sex" : 0, "age" : 13 } { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f4"), "name" : "kaily", "sex" : 0, "age" : 33 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f6"), "name" : "jack", "sex" : 1, "age" : 33 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f7"), "name" : "lily", "sex" : 0, "age" : 13 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f8"), "name" : "kaily", "sex" : 0, "age" : 33 }
5.不等于
shard1:PRIMARY> db.users1.find({age:{$ne:33}}) { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f3"), "name" : "lily", "sex" : 0, "age" : 13 } { "_id" : ObjectId("591130d79b8c9f6e4cd1c8f5"), "name" : "tom", "sex" : 1, "age" : 53 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f7"), "name" : "lily", "sex" : 0, "age" : 13 } { "_id" : ObjectId("591132899b8c9f6e4cd1c8f9"), "name" : "tom", "sex" : 1, "age" : 53 }
6.多条件取值
shard1:PRIMARY> db.users1.find({age:{$in:[13,53]}}) { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f3"), "name" : "lily", "sex" : 0, "age" : 13 } { "_id" : ObjectId("591130d79b8c9f6e4cd1c8f5"), "name" : "tom", "sex" : 1, "age" : 53 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f7"), "name" : "lily", "sex" : 0, "age" : 13 } { "_id" : ObjectId("591132899b8c9f6e4cd1c8f9"), "name" : "tom", "sex" : 1, "age" : 53 }
7.取ae为13或53或name为kaily的数据
shard1:PRIMARY> db.users1.find({or:[{age:{in:[13,53]}},{name:"kaily"}]})
{ "_id" : ObjectId("591130d59b8c9f6e4cd1c8f3"), "name" : "lily", "sex" : 0, "age" : 13 }
{ "_id" : ObjectId("591130d59b8c9f6e4cd1c8f4"), "name" : "kaily", "sex" : 0, "age" : 33 }
{ "_id" : ObjectId("591130d79b8c9f6e4cd1c8f5"), "name" : "tom", "sex" : 1, "age" : 53 }
{ "_id" : ObjectId("591132879b8c9f6e4cd1c8f7"), "name" : "lily", "sex" : 0, "age" : 13 }
{ "_id" : ObjectId("591132879b8c9f6e4cd1c8f8"), "name" : "kaily", "sex" : 0, "age" : 33 }
{ "_id" : ObjectId("591132899b8c9f6e4cd1c8f9"), "name" : "tom", "sex" : 1, "age" : 53 }
5.4 特殊查询
提示:null可以匹配自身,而且可以匹配"不存在的"
1.插入测试数据
shard1:PRIMARY> db.users1.insert({name:null,sex:1,age:18}) WriteResult({ "nInserted" : 1 }) shard1:PRIMARY> db.users1.insert({sex:1,age:24}) WriteResult({ "nInserted" : 1 })
2.查找数据
shard1:PRIMARY> db.users1.find({name:null}) #<==如下两条数据都能查到 { "_id" : ObjectId("591171619b8c9f6e4cd1c8fa"), "name" : null, "sex" : 1, "age" : 18 } { "_id" : ObjectId("5911717a9b8c9f6e4cd1c8fb"), "sex" : 1, "age" : 24 }
3.使用$exists进行查询
shard1:PRIMARY> db.users1.find({name:{in:[null],exists:true}})
{ "_id" : ObjectId("591171619b8c9f6e4cd1c8fa"), "name" : null, "sex" : 1, "age" : 18 }
5.5 数组数据查询
1. 插入测试数据
db.users1.insert({name:"wjh",sex:1,age:18,color:["red","blue","black"]}) db.users1.insert({name:"lpj",sex:1,age:22,color:["white","blue","black"]})
2.开始数组数据查询
2.1 将数组中所有包含white的文档检索出来
db.users1.find({color:"white"})
2.2 color数组中所有包含red和blue的文档都会被检索出来,数组中必须同时包含red和blue,但是他们的顺序无关紧要。
db.users1.find({color:{$all:["red","blue"]}})
2.3 精确匹配,即被检索出来的文档,color值中的数组数据必须和查询条件完全匹配,即不能多,也不能少,顺序也必须保持一致。
db.users1.find({color:["red","blue","black"]})
2.4 匹配数组中指定下标元素的值。数组的起始下标是0。注意color要加引号
db.users1.find({"color.0":"white"}) db.users1.find({"color.1":"blue"})
6. 排序
db.users1.find().sort({age:1}) db.users1.find().sort({age:1,sex:1})
提示:1代表升序,-1代表降序
7. 分页
db.users1.find().sort({age:1}).limit(3).skip(3)
提示:limit代表取多少个文档,skip代表跳过前多少个文档
8. 获取文档数量
第一种:
shard1:PRIMARY> db.users1.count() #<== 查看users1集合所有文档数量 12 shard1:PRIMARY> db.users1.count({name:null}) #<== 只查看name为空的文档数量 2
第二种:
shard1:PRIMARY> db.users1.find().count() 12 shard1:PRIMARY> db.users1.find({name:null}).count() 2
9. 删除数据
删除name为null的文档数据
shard1:PRIMARY> db.users1.remove({name:null}) WriteResult({ "nRemoved" : 2 }) #<== 删除两条
删除后查看
shard1:PRIMARY> db.users1.find({name:null}) shard1:PRIMARY> #<== 删除成功
10. 数据操作
10.1 更新数据
1.首先查看我们的数据
shard1:PRIMARY> db.users1.find() { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f2"), "name" : "jack", "sex" : 1, "age" : 33 } { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f3"), "name" : "lily", "sex" : 0, "age" : 13 } { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f4"), "name" : "kaily", "sex" : 0, "age" : 33 } { "_id" : ObjectId("591130d79b8c9f6e4cd1c8f5"), "name" : "tom", "sex" : 1, "age" : 53 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f6"), "name" : "jack", "sex" : 1, "age" : 33 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f7"), "name" : "lily", "sex" : 0, "age" : 13 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f8"), "name" : "kaily", "sex" : 0, "age" : 33 } { "_id" : ObjectId("591132899b8c9f6e4cd1c8f9"), "name" : "tom", "sex" : 1, "age" : 53 } { "_id" : ObjectId("591174be9b8c9f6e4cd1c8fc"), "name" : "wjh", "sex" : 1, "age" : 18, "color" : [ "red", "blue", "black" ] } { "_id" : ObjectId("591174cc9b8c9f6e4cd1c8fd"), "name" : "lpj", "sex" : 1, "age" : 22, "color" : [ "white", "blue", "black" ] } shard1:PRIMARY> db.users1.update({"name" : "jack"},{"name" : "jzyl"}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) shard1:PRIMARY> db.users1.find() { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f2"), "name" : "jzyl" } { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f3"), "name" : "lily", "sex" : 0, "age" : 13 } { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f4"), "name" : "kaily", "sex" : 0, "age" : 33 } { "_id" : ObjectId("591130d79b8c9f6e4cd1c8f5"), "name" : "tom", "sex" : 1, "age" : 53 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f6"), "name" : "jack", "sex" : 1, "age" : 33 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f7"), "name" : "lily", "sex" : 0, "age" : 13 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f8"), "name" : "kaily", "sex" : 0, "age" : 33 } { "_id" : ObjectId("591132899b8c9f6e4cd1c8f9"), "name" : "tom", "sex" : 1, "age" : 53 } { "_id" : ObjectId("591174be9b8c9f6e4cd1c8fc"), "name" : "wjh", "sex" : 1, "age" : 18, "color" : [ "red", "blue", "black" ] } { "_id" : ObjectId("591174cc9b8c9f6e4cd1c8fd"), "name" : "lpj", "sex" : 1, "age" : 22, "color" : [ "white", "blue", "black" ] } shard1:PRIMARY>
提示:从以上可以看出直接将name改名会将这条数据文档的field全都改掉,所以要使用如下的修改方式
shard1:PRIMARY> db.users1.update({name:"kaily"},{set:{age:66}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
shard1:PRIMARY> db.users1.find({name:"kaily"})
{ "_id" : ObjectId("591130d59b8c9f6e4cd1c8f4"), "name" : "kaily", "sex" : 0, "age" : 66 }
{ "_id" : ObjectId("591132879b8c9f6e4cd1c8f8"), "name" : "kaily", "sex" : 0, "age" : 33 }
shard1:PRIMARY> db.users1.update({_id:ObjectId("591132879b8c9f6e4cd1c8f8")},{set:{age:66}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
shard1:PRIMARY> db.users1.find({name:"kaily"})
{ "_id" : ObjectId("591130d59b8c9f6e4cd1c8f4"), "name" : "kaily", "sex" : 0, "age" : 66 }
{ "_id" : ObjectId("591132879b8c9f6e4cd1c8f8"), "name" : "kaily", "sex" : 0, "age" : 66 }
10.2 增加field
将name为kaily的增加一个field height
shard1:PRIMARY> db.users1.update({name:"kaily"},{$inc:{height:182}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) shard1:PRIMARY> db.users1.find({name:"kaily"}) { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f4"), "name" : "kaily", "sex" : 0, "age" : 66, "height" : 182 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f8"), "name" : "kaily", "sex" : 0, "age" : 66 }
10.3 将数字field增加多少增量
若存在则添加,否则更新,只能用于数字field,例如age更新前是66,更新了185,则变为251.
shard1:PRIMARY> db.users1.update({name:"kaily"},{$inc:{age:185}},true) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) shard1:PRIMARY> db.users1.find({name:"kaily"}) { "_id" : ObjectId("591130d59b8c9f6e4cd1c8f4"), "name" : "kaily", "sex" : 0, "age" : 251, "height" : 182 } { "_id" : ObjectId("591132879b8c9f6e4cd1c8f8"), "name" : "kaily", "sex" : 0, "age" : 66 }