MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。
MongoDB是一个介于关系型数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似JSON的BJSON格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是它支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。
MongoDB数据操作基于json格式
{
"userName":"admin",
"password":123456
}
下载地址:https://www.mongodb.com/try/download/community
下载的 .msi 文件,下载后双击该文件,按操作提示安装即可。
安装过程中,你可以通过点击 “Custom(自定义)” 按钮来设置你的安装目录,建议不要安装在C盘。
全程点击next安装,但是这一步需要注意,这里是安装可视化组件,默认是选择状态,这里需要取消选中,否则在安装的过程中要下载可视化组件,比较慢,甚至有时候会报错:
C:\Users\***.DESKTOP-C1RC9P2>mongo
MongoDB shell version v4.4.2-rc0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("df31999e-cb62-4f71-8a18-7db8723c514f") }
MongoDB server version: 4.4.2-rc0
---
The server generated these startup warnings when booting:
2020-10-30T16:25:16.503+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
查看所有数据库名
命令:
show dbs
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
这三个数据库是默认系统数据库,不能删除。
查看集合前提是要先指定使用哪一个数据库是用命令
命令:
ues 数据库名
show collections
> use admin
switched to db admin
> show collections
system.version
>
system.version
就是admin这个数据库中的表
查询集合中的所有数据,这里查询的是系统数据库admin中的system.version
集合
命令:
db.集合名.find()
> db.system.version.find()
{ "_id" : "featureCompatibilityVersion", "version" : "4.4" }
这里的_id是集合的键,每个集合里面默认存在,version是集合中的另一个键,相当于关系型数据库中的字段
先指定创建的数据
然后执行添加数据命令
> use company
switched to db company
> db.emp.insert({"empno":100,"ename":"admin","sex":"男","age":20,"salary":800.00,"deptno":10})
WriteResult({ "nInserted" : 1 })
这里插入了6列数据,分别是员工的编号,姓名,性别,年龄,薪资及所在的部门
WriteResult({ "nInserted" : 1 })
表示一行数据插入成功,说明创建数据成功,同时添加了一套数据,我们可以继续添加
> db.emp.insert({"empno":101,"ename":"张三","sex":"女","age":30,"salary":2500.00,"deptno":20})})})})
WriteResult({ "nInserted" : 1 })
随着数据的插入,数据库也随着创建成功。
查询所有数据,在查询数据前要先指定使用哪个数据库,再查询数据库中的所有集合,根据相应集合再查询数据。
命令:
> db.集合名.find()
指定对哪个数据库操作
> use company
switched to db company
查询指定数据库后里面的所有集合
> show collections
emp
查询所有数据,一共12条数据:
> db.emp.find()
{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }
{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }
{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }
{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 34, "salary" : 12000, "deptno" : 10 }
{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 34, "salary" : 12000, "deptno" : 10 }
{ "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }
{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 33, "salary" : 10000, "deptno" : 30 }
{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }
{ "_id" : ObjectId("5f9c371b5df291fa8194b927"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }
{ "_id" : ObjectId("5f9c376a29491ade8d9d3e79"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 32, "salary" : 1600, "deptno" : 30 }
命令:
> db.集合名.distinct("ename")
> db.emp.distinct("ename")
[
"admin",
"刘静",
"张三",
"张四",
"张良",
"李元芳",
"李元静",
"李四",
"李明",
"李菲菲",
"王三"
]
> db.emp.find({"age":25})
{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }
{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }
> db.emp.find({"ename":"李元芳"})
{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }
> db.emp.find({"age":{$gt:30}})
{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }
{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }
{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }
> db.emp.find({"age":{$lt:25}})
{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }
> db.emp.find({"age":{$gte:30}})
{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }
{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }
{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }
> db.emp.find({"age":{$lte:30}})
{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }
{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }
{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }
{ "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }
{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }
> db.emp.find({"age":{$gte:25,$lte:30}})
{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }
{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }
{ "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }
{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }
> db.emp.find({"ename":/李/})
{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }
{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }
{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }
> db.emp.find({"ename":/^张/})
{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }
{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }
> db.emp.find({"ename":/静$/})
{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }
{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }
> db.emp.find({},{"age":1})
{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "age" : 20 }
{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "age" : 30 }
{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "age" : 25 }
{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "age" : 30 }
{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "age" : 28 }
{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "age" : 32 }
{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "age" : 35 }
{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "age" : 35 }
{ "_id" : ObjectId("5f9c21d85df291fa8194b923"), "age" : 27 }
{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "age" : 35 }
{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "age" : 35 }
{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "age" : 25 }
> db.emp.find({},{"ename":1,"age":1})
{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "ename" : "admin", "age" : 20 }
{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "ename" : "张三", "age" : 30 }
{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "ename" : "张良", "age" : 25 }
{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "ename" : "李明", "age" : 30 }
{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "ename" : "李菲菲", "age" : 28 }
{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "ename" : "张四", "age" : 32 }
{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "ename" : "李四", "age" : 35 }
{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "ename" : "李四", "age" : 35 }
{ "_id" : ObjectId("5f9c21d85df291fa8194b923"), "ename" : "王三", "age" : 27 }
{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "ename" : "李元芳", "age" : 35 }
{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "ename" : "李元静", "age" : 35 }
{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "ename" : "刘静", "age" : 25 }
> db.emp.find({"age":{$gte:30}},{"ename":1,"age":1,"salary":1})
{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "ename" : "张三", "age" : 30, "salary" : 2500 }
{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "ename" : "李明", "age" : 30, "salary" : 1800 }
{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "ename" : "张四", "age" : 32, "salary" : 8000 }
{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "ename" : "李四", "age" : 35, "salary" : 12000 }
{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "ename" : "李四", "age" : 35, "salary" : 12000 }
{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "ename" : "李元芳", "age" : 35, "salary" : 8000 }
{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "ename" : "李元静", "age" : 35, "salary" : 15000 }
> db.emp.find().sort({"age":1})
{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }
{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }
{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }
{ "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }
{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }
{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }
{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }
{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }
> db.emp.find().sort({salary:-1})
{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }
{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }
{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }
{ "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }
{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }
{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }
{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }
{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }
> db.emp.find({ename:"admin",age:20})
{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }
> db.emp.find().limit(5)
{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }
{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }
{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }
> db.emp.find().skip(10)
{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }
{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }
> db.emp.find().count()
12
> db.emp.find({salary:{$gte:5000}}).count()
6
> db.emp.find({salary:{$gte:5000}})
{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }
{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
{ "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }
{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }
{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }
第一页:
> db.emp.find().skip(0).limit(5)
{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }
{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }
{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }
第二页:
> db.emp.find().skip(5).limit(5)
{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }
{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
{ "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }
{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }
第三页:
> db.emp.find().skip(10).limit(5)
{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }
{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }
> db.emp.find({$or:[{age:30},{age:35}]})
{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }
{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }
> db.emp.findOne()
{
"_id" : ObjectId("5f9c1c5b5df291fa8194b91b"),
"empno" : 100,
"ename" : "admin",
"sex" : "男",
"age" : 20,
"salary" : 800,
"deptno" : 10
}
或
> db.emp.find().limit(1)
{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }
> db.emp.getDB()
company
$set
否则会全部修改
> db.emp.update({ename:"李元静"},{$set:{age:32}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.emp.find({ename:"李元静"})
{ "_id" : ObjectId("5f9c376a29491ade8d9d3e79"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 32, "salary" : 15000, "deptno" : 36 }
> db.emp.update({ename:"李元静"},{$set:{salary:1600,deptno:30}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.emp.find({ename:"李元静"})
{ "_id" : ObjectId("5f9c376a29491ade8d9d3e79"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 32, "salary" : 1600, "deptno" : 30 }
{multi:true}
> db.emp.update({age:35},{$set:{age:34}},{multi:true})
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
db.emp.find()
验证所有数据没有年龄为35的员工
> db.emp.update({ename:"李元芳"},{$inc:{age:-1,salary:2000}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.emp.find({ename:"李元芳"})
{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 33, "salary" : 10000, "deptno" : 30 }
db.emp.remove({ename:"李元芳"})
db.emp.remove({})
db.emp.drop()
db.dropDatabase()
> use com
switched to db com
> for(var i=0;i<600000;i++){
... db.users.insert({userNo:i,userName:"张三"+i,age:28,phone:"13000"+i})
... }
WriteResult({ "nInserted" : 1 })
> db.users.find().count()
600000
命令:
db.users.find({userNo:599999}).explain("executionStats")
> db.users.find({userNo:599999}).explain("executionStats")
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "com.users",
"indexFilterSet" : false,
"parsedQuery" : {
"userNo" : {
"$eq" : 599999
}
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"userNo" : {
"$eq" : 599999
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 254,
"totalKeysExamined" : 0,
"totalDocsExamined" : 600000,
"executionStages" : {
"stage" : "COLLSCAN",
"filter" : {
"userNo" : {
"$eq" : 599999
}
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 3,
"works" : 600002,
"advanced" : 1,
"needTime" : 600000,
"needYield" : 0,
"saveState" : 600,
"restoreState" : 600,
"isEOF" : 1,
"direction" : "forward",
"docsExamined" : 600000
}
},
"serverInfo" : {
"host" : "thinkPadE580",
"port" : 27017,
"version" : "4.4.2-rc0",
"gitVersion" : "b5fafa1f87dda6f8773c5a8a1a5e7776d4d94da7"
},
"ok" : 1
}
"executionTimeMillis" : 254
可以知道查询所耗费时间为254毫秒,当然这与计算机配置性能有关。
命令:
db.users.ensureIndex({userNo:1})
> db.users.ensureIndex({userNo:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
命令:
db.users.getIndexes()
> db.users.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"userNo" : 1
},
"name" : "userNo_1"
}
]
> db.users.find({userNo:599999}).explain("executionStats")
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "com.users",
"indexFilterSet" : false,
"parsedQuery" : {
"userNo" : {
"$eq" : 599999
}
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"userNo" : 1
},
"indexName" : "userNo_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"userNo" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"userNo" : [
"[599999.0, 599999.0]"
]
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 84,
"totalKeysExamined" : 1,
"totalDocsExamined" : 1,
"executionStages" : {
"stage" : "FETCH",
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 2,
"advanced" : 1,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"docsExamined" : 1,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 2,
"advanced" : 1,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"keyPattern" : {
"userNo" : 1
},
"indexName" : "userNo_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"userNo" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"userNo" : [
"[599999.0, 599999.0]"
]
},
"keysExamined" : 1,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0
}
}
},
"serverInfo" : {
"host" : "thinkPadE580",
"port" : 27017,
"version" : "4.4.2-rc0",
"gitVersion" : "b5fafa1f87dda6f8773c5a8a1a5e7776d4d94da7"
},
"ok" : 1
}
"executionTimeMillisEstimate" : 0
知道,所耗费的时间为0毫秒,大大缩短了查询速度
命令:
db.users.dropIndex({userNo:1})
验证
> db.users.dropIndex({userNo:1})
{ "nIndexesWas" : 2, "ok" : 1 }
> db.users.ensureIndex({userNo:1,userName:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.users.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"userNo" : 1,
"userName" : 1
},
"name" : "userNo_1_userName_1"
}
]
> db.users.find({userNo:599999,userName:"张三599999"}).explain("executionStats")))
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "com.users",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"userName" : {
"$eq" : "张三599999"
}
},
{
"userNo" : {
"$eq" : 599999
}
}
]
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"userNo" : 1,
"userName" : 1
},
"indexName" : "userNo_1_userName_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"userNo" : [ ],
"userName" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"userNo" : [
"[599999.0, 599999.0]"
],
"userName" : [
"[\"张三599999\", \"张三599999\"]"
]
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 2,
"totalKeysExamined" : 1,
"totalDocsExamined" : 1,
"executionStages" : {
"stage" : "FETCH",
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 2,
"advanced" : 1,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"docsExamined" : 1,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 2,
"advanced" : 1,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"keyPattern" : {
"userNo" : 1,
"userName" : 1
},
"indexName" : "userNo_1_userName_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"userNo" : [ ],
"userName" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"userNo" : [
"[599999.0, 599999.0]"
],
"userName" : [
"[\"张三599999\", \"张三599999\"]"
]
},
"keysExamined" : 1,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0
}
}
},
"serverInfo" : {
"host" : "thinkPadE580",
"port" : 27017,
"version" : "4.4.2-rc0",
"gitVersion" : "b5fafa1f87dda6f8773c5a8a1a5e7776d4d94da7"
},
"ok" : 1
}
通过"executionTimeMillis" : 2,
可以看出,查询userNo和userName只需要2毫秒
注意:如果在一个集合中,对多个字段设置索引N(N!=1),在使用复合索引查询的时候,要连同第一个索引字段一起查询,如果只单单查询第N个,索引将没有效果。
创建唯一索引的条件是,集合中字段的数据不能重复,但在缺省情况下创建是索引均不是唯一索引
由于集合中的age都是一样的值,在给age创建唯一索引的时候会报错
命令:
> db.users.ensure({age:1},{unique:true})
> db.users.ensure({age:1},{unique:true})
TypeError: db.users.ensure is not a function :
@(shell):1:1