MongoDB数据库—基本操作

一、数据库操作
1. 查看所有数据库

我们可以使用如下语句查看MongoDB中所有的数据库,如下:

> show dbs
admin  (empty)
local  0.078GB
2. 创建数据库

格式如下:

use database_name

注意:

  1. 如果数据库不存在则创建数据库,否则切换到指定的数据库
  2. 如果刚刚创建的数据库不在列表内,如果要显示它,我们需要向刚刚创建的数据库中插入一些数据

例如:

> show dbs
admin  (empty)
local  0.078GB

> use school
switched to db school
3. 删除数据库

格式:

db.dropDatabase()

注意:前提条件是正在使用当前数据库

例如:

> use school
switched to db school

> db.dropDatabase()
{ "dropped" : "school", "ok" : 1 }
4. 查看当前正在使用的数据库

可以使用如下两种方法进行查询当前正在使用的数据库

第一种方法:

> db
school

第二种方法:

> db.getName()
school
5. 断开连接
> exit
bye
6. 查看命令api

可以使用help查询api

> help
二、集合操作
1. 查看当前数据库下有哪些集合

我们可以使用如下语句查看当前数据库下有哪些集合

> show collections
2. 创建集合

格式:

db.collection_name.insert(doc)

注意:如果集合不存在,则自动创建集合并将文档插入,如集合存在,则直接将文档插入

例如:

> show collections

> db.student.insert({name:'叶无道',sex:'男',age:25,score:92})
WriteResult({ "nInserted" : 1 })

> show collections
student
system.indexes
3. 删除当前数据库中的集合

格式:

db.collection_name.drop()

当集合存在时,删除集合会返回true,否则返回false,如下:

> show collections
Class
student
system.indexes

> db.Class.drop()
true

> db.Class.drop()
false

> show collections
student
system.indexes
三、文档操作
1. 插入文档:insert()和save()

(1) 使用insert()方法插入文档

格式1:插入一个文档

db.collection_name.insert(doc)

格式2:插入多个文档

db.collection_name.insert(doc1,doc2,...,docN)

例1:插入一个学生

> db.student.insert({name:'慕容雪痕',sex:'女',age:23,score:98})
WriteResult({ "nInserted" : 1 })

例2:插入多个学生

> db.student.insert([{name:'杨宁素',sex:'女',age:36,score:95},{name:'苏惜水',sex:'女',age:37,score:90}])
BulkWriteResult({
	"writeErrors" : [ ],
	"writeConcernErrors" : [ ],
	"nInserted" : 2,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})

(2) 使用save()方法插入文档

格式:

db.collection_name.save(doc)

注意:如果指定_id字段,则会更新_id字段的数据;不指定_id字段,save()方法类似于insert()方法

例1:

> db.student.save({name:'蔡羽绾',sex:'女',age:28,score:89})
WriteResult({ "nInserted" : 1 })

例2:

db.student.save({"_id" : ObjectId("5d27285989a435a8bca30e8e"),name:'蔡羽绾',sex:'女',age:30,score:86})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
2. 文档更新

update()方法用于更新已存在的文档

格式:

db.collection_name.update(query,update,{upsert:<boolean>, multi:<boolean>,})

参数说明:

  1. query:update的查询条件,类似于sql里update语句内where后面的内容
  2. update:update的对象和一些更新的操作符($set,$inc)等,$set直接更新,$inc在原有的基础上累加后更新
  3. upsert:可选,如果不存在update的记录,是否当新数据插入,true为插入,false为不插入,默认为false
  4. multi:可选,mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就按照条件查找出来的数据全部更新

例1:将慕容雪痕的年龄更新为21

> db.student.update({name:'慕容雪痕'},{$set:{age:21}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

例2:将杨宁素的年龄减3岁

> db.student.update({name:'杨宁素'},{$inc:{age:-3}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

例3:将女生的年龄都改为18

> db.student.update({sex:'女'},{$set:{age:18}},{multi:true})
WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 4 })

例4:如果不能匹配则插入,如果匹配则更新多条

> db.student.update({name:'赵清思'},{$set:{name:'赵清思',sex:'女',age:20,score:88}},true,true)
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 1,
	"nModified" : 0,
	"_id" : ObjectId("5d2730ef76728d7ac5650485")
})

> db.student.update({sex:'女'},{$set:{score:99}},true,true)
WriteResult({ "nMatched" : 5, "nUpserted" : 0, "nModified" : 5 })
3. 文档删除

格式:

db.集合名.remove(query,{justOne:<boolean>,})

参数说明:

  1. query:可选,删除的文档的条件
  2. justOne:可选,如果为true或1,则只删除一个文档

建议:在执行remove()函数前,先执行find()命令来判断执行的条件是否存在是一个良好习惯

例1:删除一个文档

> db.student.remove({score:98},{justOne:true})
WriteResult({ "nRemoved" : 1 })

例2:删除多个文档

> db.student.remove({score:98})
WriteResult({ "nRemoved" : 4 })
4. 文档查询

(1) 查询集合下所有的文档(数据)

格式:

db.collection_name.find() 

例如:

> db.student.find()
{ "_id" : ObjectId("5d2708ae3c9a378bf66cf64a"), "name" : "叶无道", "sex" : "男", "age" : 25, "score" : 92 }
{ "_id" : ObjectId("5d2716af3c9a378bf66cf64c"), "name" : "慕容雪痕", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d271d28c7091979f2521ffb"), "name" : "杨宁素", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d271d28c7091979f2521ffc"), "name" : "苏惜水", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d27285989a435a8bca30e8e"), "name" : "蔡羽绾", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d2730ef76728d7ac5650485"), "name" : "赵清思", "sex" : "女", "age" : 20, "score" : 98 }

(2) 查询指定列

格式:

db.collection_name.find(query,{<key>:1,<key>:1...})

参数说明:

  1. query:查询条件
  2. key:要显示的字段,1表示显示

例1:查询所有的name和score

> db.student.find({},{name:1,score:1})
{ "_id" : ObjectId("5d2708ae3c9a378bf66cf64a"), "name" : "叶无道", "score" : 92 }
{ "_id" : ObjectId("5d2716af3c9a378bf66cf64c"), "name" : "慕容雪痕", "score" : 98 }
{ "_id" : ObjectId("5d271d28c7091979f2521ffb"), "name" : "杨宁素", "score" : 98 }
{ "_id" : ObjectId("5d271d28c7091979f2521ffc"), "name" : "苏惜水", "score" : 98 }
{ "_id" : ObjectId("5d27285989a435a8bca30e8e"), "name" : "蔡羽绾", "score" : 98 }
{ "_id" : ObjectId("5d2730ef76728d7ac5650485"), "name" : "赵清思", "score" : 98 }

例2:查询所有男生的name和score

> db.student.find({sex:'男'},{name:1,score:1})
{ "_id" : ObjectId("5d2708ae3c9a378bf66cf64a"), "name" : "叶无道", "score" : 92 }

(3) pretty()方法以格式化的方式来显示文档

> db.student.find({sex:'男'},{name:1,score:1}).pretty()
{
	"_id" : ObjectId("5d2708ae3c9a378bf66cf64a"),
	"name" : "叶无道",
	"score" : 92
}

(4) findOne()方法查询匹配结果的第一条数据

> db.student.findOne({age:18})
{
	"_id" : ObjectId("5d2716af3c9a378bf66cf64c"),
	"name" : "慕容雪痕",
	"sex" : "女",
	"age" : 18,
	"score" : 98
}

(5) 查询某个结果集的数据条数

> db.student.find().count()
6
5. 查询条件操作符

作用:条件操作符用于比较两个表达式并从Mongodb集合中获取数据

格式:

db.collection_name.find({<key>:{operator:<value>}})

(1) 大于($gt)

> db.student.find({age:{$gt:20}})
{ "_id" : ObjectId("5d2708ae3c9a378bf66cf64a"), "name" : "叶无道", "sex" : "男", "age" : 25, "score" : 92 }

(2) 大于等于($gte)

> db.student.find({age:{$gte:20}})
{ "_id" : ObjectId("5d2708ae3c9a378bf66cf64a"), "name" : "叶无道", "sex" : "男", "age" : 25, "score" : 92 }
{ "_id" : ObjectId("5d2730ef76728d7ac5650485"), "name" : "赵清思", "sex" : "女", "age" : 20, "score" : 98 }

(3) 小于($lt)

> db.student.find({age:{$lt:20}})
{ "_id" : ObjectId("5d2716af3c9a378bf66cf64c"), "name" : "慕容雪痕", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d271d28c7091979f2521ffb"), "name" : "杨宁素", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d271d28c7091979f2521ffc"), "name" : "苏惜水", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d27285989a435a8bca30e8e"), "name" : "蔡羽绾", "sex" : "女", "age" : 18, "score" : 98 }

(4) 小于等于($lte)

> db.student.find({age:{$lte:20}})
{ "_id" : ObjectId("5d2716af3c9a378bf66cf64c"), "name" : "慕容雪痕", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d271d28c7091979f2521ffb"), "name" : "杨宁素", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d271d28c7091979f2521ffc"), "name" : "苏惜水", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d27285989a435a8bca30e8e"), "name" : "蔡羽绾", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d2730ef76728d7ac5650485"), "name" : "赵清思", "sex" : "女", "age" : 20, "score" : 98 }

(5) 等于(:)

> db.student.find({age:20})
{ "_id" : ObjectId("5d2730ef76728d7ac5650485"), "name" : "赵清思", "sex" : "女", "age" : 20, "score" : 98 }

(6) 小于等于和大于等于连用

> db.student.find({age:{$gte:20,$lte:25}})
{ "_id" : ObjectId("5d2708ae3c9a378bf66cf64a"), "name" : "叶无道", "sex" : "男", "age" : 25, "score" : 92 }
{ "_id" : ObjectId("5d2730ef76728d7ac5650485"), "name" : "赵清思", "sex" : "女", "age" : 20, "score" : 98 }

(7) 使用_id进行查询

> db.student.find({_id:ObjectId("5d2716af3c9a378bf66cf64c")})
{ "_id" : ObjectId("5d2716af3c9a378bf66cf64c"), "name" : "慕容雪痕", "sex" : "女", "age" : 18, "score" : 98 }

(8) 查询某个字段的值当中是否包含另一个值

> db.student.find({name://})
{ "_id" : ObjectId("5d2716af3c9a378bf66cf64c"), "name" : "慕容雪痕", "sex" : "女", "age" : 18, "score" : 98 }

(9) 查询某个字段的值是否以另一个值开头

> db.student.find({name:/^/})
{ "_id" : ObjectId("5d2708ae3c9a378bf66cf64a"), "name" : "叶无道", "sex" : "男", "age" : 25, "score" : 92 }
6. 条件查询:and和or

(1) and条件

格式:

db.collection_name.find({条件1,条件2,……,条件n})

例:查询年龄大于等于20的女生

> db.student.find({age:{$gte:20},sex:'女'})
{ "_id" : ObjectId("5d2730ef76728d7ac5650485"), "name" : "赵清思", "sex" : "女", "age" : 20, "score" : 98 }

(2) or条件

格式:

 db.集合名.find({$or:[{条件1},{条件2},……,{条件n}]})

例:查询age=20或者sex=男的学生

> db.student.find({$or:[{age:20},{sex:'男'}]})
{ "_id" : ObjectId("5d2708ae3c9a378bf66cf64a"), "name" : "叶无道", "sex" : "男", "age" : 25, "score" : 92 }
{ "_id" : ObjectId("5d2730ef76728d7ac5650485"), "name" : "赵清思", "sex" : "女", "age" : 20, "score" : 98 }

(3) and和or的联合使用

例:查询所有男生或者年龄20的女生

> db.student.find({$or:[{sex:'男'},{age:20,sex:'女'}]})
{ "_id" : ObjectId("5d2708ae3c9a378bf66cf64a"), "name" : "叶无道", "sex" : "男", "age" : 25, "score" : 92 }
{ "_id" : ObjectId("5d2730ef76728d7ac5650485"), "name" : "赵清思", "sex" : "女", "age" : 20, "score" : 98 }
7. limit和skip

(1) limit:读取指定数量的数据记录

> db.student.find().limit(3)
{ "_id" : ObjectId("5d2708ae3c9a378bf66cf64a"), "name" : "叶无道", "sex" : "男", "age" : 25, "score" : 92 }
{ "_id" : ObjectId("5d2716af3c9a378bf66cf64c"), "name" : "慕容雪痕", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d271d28c7091979f2521ffb"), "name" : "杨宁素", "sex" : "女", "age" : 18, "score" : 98 }

(2) skip:跳过指定数量的数据

> db.student.find().skip(4)
{ "_id" : ObjectId("5d27285989a435a8bca30e8e"), "name" : "蔡羽绾", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d2730ef76728d7ac5650485"), "name" : "赵清思", "sex" : "女", "age" : 20, "score" : 98 }

(3) skip与limit联合使用

通常用这种方式来实现分页功能

例:每页显示4个文档

> db.student.find().skip(0).limit(4)
{ "_id" : ObjectId("5d2708ae3c9a378bf66cf64a"), "name" : "叶无道", "sex" : "男", "age" : 25, "score" : 92 }
{ "_id" : ObjectId("5d2716af3c9a378bf66cf64c"), "name" : "慕容雪痕", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d271d28c7091979f2521ffb"), "name" : "杨宁素", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d271d28c7091979f2521ffc"), "name" : "苏惜水", "sex" : "女", "age" : 18, "score" : 98 }

> db.student.find().skip(4).limit(4)
{ "_id" : ObjectId("5d27285989a435a8bca30e8e"), "name" : "蔡羽绾", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d2730ef76728d7ac5650485"), "name" : "赵清思", "sex" : "女", "age" : 20, "score" : 98 }
8. 排序

格式:

db.collection_name.find().sort({<key>:1|-1})

说明:1表示升序,-1表示降序

例:按照score值升序排序,若score值相等则按name值升序排序

> db.student.find().sort({score:1,name:1})
{ "_id" : ObjectId("5d2708ae3c9a378bf66cf64a"), "name" : "叶无道", "sex" : "男", "age" : 25, "score" : 92 }
{ "_id" : ObjectId("5d2716af3c9a378bf66cf64c"), "name" : "慕容雪痕", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d271d28c7091979f2521ffb"), "name" : "杨宁素", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d271d28c7091979f2521ffc"), "name" : "苏惜水", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d27285989a435a8bca30e8e"), "name" : "蔡羽绾", "sex" : "女", "age" : 18, "score" : 98 }
{ "_id" : ObjectId("5d2730ef76728d7ac5650485"), "name" : "赵清思", "sex" : "女", "age" : 20, "score" : 98 }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值