Blogger Information
Blog 2
fans 0
comment 0
visits 1238
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
MongoDB基础教程
P粉469731340
Original
521 people have browsed it

MongoDB 4.X教程

一、MongoDB介绍

MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。

MongoDB是一个介于关系型数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似JSON的BJSON格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是它支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。

MongoDB数据操作基于json格式

  1. {
  2. "userName":"admin",
  3. "password":123456
  4. }

二、MongoDB安装

1. MongoDB下载

下载地址:https://www.mongodb.com/try/download/community

2.MongoDB安装

  • 下载的 .msi 文件,下载后双击该文件,按操作提示安装即可。

  • 安装过程中,你可以通过点击 “Custom(自定义)” 按钮来设置你的安装目录,建议不要安装在C盘。

  • img

  • 全程点击next安装,但是这一步需要注意,这里是安装可视化组件,默认是选择状态,这里需要取消选中,否则在安装的过程中要下载可视化组件,比较慢,甚至有时候会报错:

3.MongoDB环境变量配置

  • 在桌面右键 此电脑>>>属性>>>高级系统设置>>>高级>>>环境变量>>>找到path>>>选择编辑>>>新建

  • 在打开的环境变量中MongoDB安装的bin路径复制到新建目录中

  • 然后点击所有的确定即可完成环境变量配置

4.验证安装是否成功

  • 打开CMD命令窗口,输入mongo,出现以下提示信息,说明安装成功。
  1. C:\Users\***.DESKTOP-C1RC9P2>mongo
  2. MongoDB shell version v4.4.2-rc0
  3. connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
  4. Implicit session: session { "id" : UUID("df31999e-cb62-4f71-8a18-7db8723c514f") }
  5. MongoDB server version: 4.4.2-rc0
  6. ---
  7. The server generated these startup warnings when booting:
  8. 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
  9. ---
  10. ---
  11. Enable MongoDB's free cloud-based monitoring service, which will then receive and display
  12. metrics about your deployment (disk utilization, CPU, operation statistics, etc).
  13. The monitoring data will be available on a MongoDB website with a unique URL accessible to you
  14. and anyone you share the URL with. MongoDB may use this information to make product
  15. improvements and to suggest MongoDB products and deployment options to you.
  16. To enable free monitoring, run the following command: db.enableFreeMonitoring()
  17. To permanently disable this reminder, run the following command: db.disableFreeMonitoring()

三、MongoDB使用基本介绍

  • MongoDB属于非关系型数据库,其数据库、表、字段等和关系型数据库(如:MySQL数据库)有一定的差别;

  • MongoDB中的集合就相当于关系型数据库中的表

  • MongoDB中的json字符串的键相当于关系型数据库中的列名;

  • 在操作MongoDB数据的时候全部使用json数据格式。

1.查看数据库名

  • 查看所有数据库名

    1. 命令:
    2. show dbs
    1. > show dbs
    2. admin 0.000GB
    3. config 0.000GB
    4. local 0.000GB
  • 这三个数据库是默认系统数据库,不能删除。

2. 查看集合

  • 查看集合前提是要先指定使用哪一个数据库是用命令

    1. 命令:
    2. ues 数据库名
    3. show collections
    1. > use admin
    2. switched to db admin
    3. > show collections
    4. system.version
    5. >
  • system.version就是admin这个数据库中的表

3.查询集合中的数据

  • 这里先简单介绍查询集合中的所有数据,方便后面学习。

  • 查询集合中的所有数据,这里查询的是系统数据库admin中的system.version集合

    1. 命令:
    2. db.集合名.find()
    1. > db.system.version.find()
    2. { "_id" : "featureCompatibilityVersion", "version" : "4.4" }
  • 这里的_id是集合的键,每个集合里面默认存在,version是集合中的另一个键,相当于关系型数据库中的字段

四、创建数据库及添加数据

  • MongoDB不能够直接创建数据库,需要添加一条数据才能创建

1.创建数据库和插入数据

  • 先指定创建的数据

  • 然后执行添加数据命令

    1. > use company
    2. switched to db company
    3. > db.emp.insert({"empno":100,"ename":"admin","sex":"男","age":20,"salary":800.00,"deptno":10})
    4. WriteResult({ "nInserted" : 1 })
  • 这里插入了6列数据,分别是员工的编号,姓名,性别,年龄,薪资及所在的部门

  • WriteResult({ "nInserted" : 1 })表示一行数据插入成功,说明创建数据成功,同时添加了一套数据,我们可以继续添加

    1. > db.emp.insert({"empno":101,"ename":"张三","sex":"女","age":30,"salary":2500.00,"deptno":20})})})})
    2. WriteResult({ "nInserted" : 1 })
  • 随着数据的插入,数据库也随着创建成功。

五、查询数据

查询所有数据,在查询数据前要先指定使用哪个数据库,再查询数据库中的所有集合,根据相应集合再查询数据。

1. 查询所有数据

  1. 命令:
  2. > db.集合名.find()
  1. 指定对哪个数据库操作
  2. > use company
  3. switched to db company
  4. 查询指定数据库后里面的所有集合
  5. > show collections
  6. emp
  7. 查询所有数据,一共12条数据:
  8. > db.emp.find()
  9. { "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }
  10. { "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
  11. { "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }
  12. { "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
  13. { "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }
  14. { "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 34, "salary" : 12000, "deptno" : 10 }
  15. { "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 34, "salary" : 12000, "deptno" : 10 }
  16. { "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }
  17. { "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 33, "salary" : 10000, "deptno" : 30 }
  18. { "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }
  19. { "_id" : ObjectId("5f9c371b5df291fa8194b927"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }
  20. { "_id" : ObjectId("5f9c376a29491ade8d9d3e79"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 32, "salary" : 1600, "deptno" : 30 }

2.去掉集合中重复的数据

  1. 命令:
  2. > db.集合名.distinct("ename")
  1. > db.emp.distinct("ename")
  2. [
  3. "admin",
  4. "刘静",
  5. "张三",
  6. "张四",
  7. "张良",
  8. "李元芳",
  9. "李元静",
  10. "李四",
  11. "李明",
  12. "李菲菲",
  13. "王三"
  14. ]

3. 查询年龄等于25的数据

  • 这里的“age”可以不加“ ”,直接写成{age:25}
  1. > db.emp.find({"age":25})
  2. { "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }
  3. { "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }

4. 查询ename=“李元芳“的数据

  1. > db.emp.find({"ename":"李元芳"})
  2. { "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }

5. 查询age>30岁的员工数据

  1. > db.emp.find({"age":{$gt:30}})
  2. { "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }
  3. { "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
  4. { "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
  5. { "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }
  6. { "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }

6. 查询age<25的员工数据

  1. > db.emp.find({"age":{$lt:25}})
  2. { "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }

7.查询age>=30的员工数据

  1. > db.emp.find({"age":{$gte:30}})
  2. { "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
  3. { "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
  4. { "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }
  5. { "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
  6. { "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
  7. { "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }
  8. { "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }

8. 查询age<=30的员工数据

  1. > db.emp.find({"age":{$lte:30}})
  2. { "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }
  3. { "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
  4. { "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }
  5. { "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
  6. { "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }
  7. { "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }
  8. { "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }

9. 查询age>=25并且age<=30的员工数据

  1. > db.emp.find({"age":{$gte:25,$lte:30}})
  2. { "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
  3. { "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }
  4. { "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
  5. { "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }
  6. { "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }
  7. { "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }

10. 查询全部姓李的员工数据(模糊查询)

  • 模糊查询一般用于查询数据量比较小的集合数据
  1. > db.emp.find({"ename":/李/})
  2. { "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
  3. { "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }
  4. { "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
  5. { "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
  6. { "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }
  7. { "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }

11. 查询姓名以“张”开头的员工数据

  1. > db.emp.find({"ename":/^张/})
  2. { "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
  3. { "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }
  4. { "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }

12. 查询以姓名以“静”结尾的员工数据

  1. > db.emp.find({"ename":/静$/})
  2. { "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }
  3. { "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }

13. 查询指定列的员工数据

  • 查询所有员工年龄的数据
  1. > db.emp.find({},{"age":1})
  2. { "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "age" : 20 }
  3. { "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "age" : 30 }
  4. { "_id" : ObjectId("5f9c20945df291fa8194b91d"), "age" : 25 }
  5. { "_id" : ObjectId("5f9c21055df291fa8194b91e"), "age" : 30 }
  6. { "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "age" : 28 }
  7. { "_id" : ObjectId("5f9c21855df291fa8194b920"), "age" : 32 }
  8. { "_id" : ObjectId("5f9c219b5df291fa8194b921"), "age" : 35 }
  9. { "_id" : ObjectId("5f9c21a75df291fa8194b922"), "age" : 35 }
  10. { "_id" : ObjectId("5f9c21d85df291fa8194b923"), "age" : 27 }
  11. { "_id" : ObjectId("5f9c22185df291fa8194b924"), "age" : 35 }
  12. { "_id" : ObjectId("5f9c22445df291fa8194b925"), "age" : 35 }
  13. { "_id" : ObjectId("5f9c227d5df291fa8194b926"), "age" : 25 }
  • 查询所有员工的姓名和年龄的数据
  1. > db.emp.find({},{"ename":1,"age":1})
  2. { "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "ename" : "admin", "age" : 20 }
  3. { "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "ename" : "张三", "age" : 30 }
  4. { "_id" : ObjectId("5f9c20945df291fa8194b91d"), "ename" : "张良", "age" : 25 }
  5. { "_id" : ObjectId("5f9c21055df291fa8194b91e"), "ename" : "李明", "age" : 30 }
  6. { "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "ename" : "李菲菲", "age" : 28 }
  7. { "_id" : ObjectId("5f9c21855df291fa8194b920"), "ename" : "张四", "age" : 32 }
  8. { "_id" : ObjectId("5f9c219b5df291fa8194b921"), "ename" : "李四", "age" : 35 }
  9. { "_id" : ObjectId("5f9c21a75df291fa8194b922"), "ename" : "李四", "age" : 35 }
  10. { "_id" : ObjectId("5f9c21d85df291fa8194b923"), "ename" : "王三", "age" : 27 }
  11. { "_id" : ObjectId("5f9c22185df291fa8194b924"), "ename" : "李元芳", "age" : 35 }
  12. { "_id" : ObjectId("5f9c22445df291fa8194b925"), "ename" : "李元静", "age" : 35 }
  13. { "_id" : ObjectId("5f9c227d5df291fa8194b926"), "ename" : "刘静", "age" : 25 }
  • 查询age>=30的员工姓名、年龄和薪资
  1. > db.emp.find({"age":{$gte:30}},{"ename":1,"age":1,"salary":1})
  2. { "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "ename" : "张三", "age" : 30, "salary" : 2500 }
  3. { "_id" : ObjectId("5f9c21055df291fa8194b91e"), "ename" : "李明", "age" : 30, "salary" : 1800 }
  4. { "_id" : ObjectId("5f9c21855df291fa8194b920"), "ename" : "张四", "age" : 32, "salary" : 8000 }
  5. { "_id" : ObjectId("5f9c219b5df291fa8194b921"), "ename" : "李四", "age" : 35, "salary" : 12000 }
  6. { "_id" : ObjectId("5f9c21a75df291fa8194b922"), "ename" : "李四", "age" : 35, "salary" : 12000 }
  7. { "_id" : ObjectId("5f9c22185df291fa8194b924"), "ename" : "李元芳", "age" : 35, "salary" : 8000 }
  8. { "_id" : ObjectId("5f9c22445df291fa8194b925"), "ename" : "李元静", "age" : 35, "salary" : 15000 }

14. 排序

  • 按照年龄升序排列
  1. > db.emp.find().sort({"age":1})
  2. { "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }
  3. { "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }
  4. { "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }
  5. { "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }
  6. { "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }
  7. { "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
  8. { "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
  9. { "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }
  10. { "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
  11. { "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
  12. { "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }
  13. { "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }
  • 按照薪资降序排列
  1. > db.emp.find().sort({salary:-1})
  2. { "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }
  3. { "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
  4. { "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
  5. { "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }
  6. { "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }
  7. { "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }
  8. { "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }
  9. { "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }
  10. { "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }
  11. { "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
  12. { "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
  13. { "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }

15. 查询ename=admin且age=20的员工数据

  1. > db.emp.find({ename:"admin",age:20})
  2. { "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }

16. 查询前5条数据

  1. > db.emp.find().limit(5)
  2. { "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }
  3. { "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
  4. { "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }
  5. { "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
  6. { "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }

17. 查询10条以后的数据

  • 这里一共12条数据,查询10条以后的数据,结果是两条数据。
  1. > db.emp.find().skip(10)
  2. { "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }
  3. { "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }

18. 查询集合中的总数据

  • 一共12条数据
  1. > db.emp.find().count()
  2. 12
  • 查询salary>=5000的员工数量
  1. > db.emp.find({salary:{$gte:5000}}).count()
  2. 6
  • 验证(查询查询salary>=5000的员工数据,一共是6条。)
  1. > db.emp.find({salary:{$gte:5000}})
  2. { "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }
  3. { "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
  4. { "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
  5. { "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }
  6. { "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }
  7. { "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }

19. 分页查询

  • 按照每页显示5条数据查询,一共12条数据,就要查询3页
  1. 第一页:
  2. > db.emp.find().skip(0).limit(5)
  3. { "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }
  4. { "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
  5. { "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }
  6. { "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
  7. { "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }
  8. 第二页:
  9. > db.emp.find().skip(5).limit(5)
  10. { "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }
  11. { "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
  12. { "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
  13. { "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }
  14. { "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }
  15. 第三页:
  16. > db.emp.find().skip(10).limit(5)
  17. { "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }
  18. { "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }
  • skip的值=(页数-1)* 每页显示数量
  • skip(N):表示要查询第N条数据后的数据

20.关键字or的查询方式

  • 查询年龄是30或者年龄是35的员工数据(注意写法)
  1. > db.emp.find({$or:[{age:30},{age:35}]})
  2. { "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }
  3. { "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }
  4. { "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
  5. { "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }
  6. { "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }
  7. { "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }

21. 查询第一条数据

  1. > db.emp.findOne()
  2. {
  3. "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"),
  4. "empno" : 100,
  5. "ename" : "admin",
  6. "sex" : "男",
  7. "age" : 20,
  8. "salary" : 800,
  9. "deptno" : 10
  10. }

  1. > db.emp.find().limit(1)
  2. { "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }

22. 查询当前表所在的数据库

  1. > db.emp.getDB()
  2. company

六、更新数据

  • 更新数据一定要有条件限制,并且需要加上$set否则会全部修改
  • 第一个参数是条件,后面的参数是要修改的数据或者是其他操作,比如批量操作

1. 更新员工李元静的年龄为32岁

  1. > db.emp.update({ename:"李元静"},{$set:{age:32}})
  2. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  • 验证(查询姓名是李元静的员工数据)
  1. > db.emp.find({ename:"李元静"})
  2. { "_id" : ObjectId("5f9c376a29491ade8d9d3e79"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 32, "salary" : 15000, "deptno" : 36 }

2. 更新员工李元静的薪资为1600并且所在部门修改为30

  1. > db.emp.update({ename:"李元静"},{$set:{salary:1600,deptno:30}})
  2. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  • 验证(查询姓名是李元静的员工数据)
  1. > db.emp.find({ename:"李元静"})
  2. { "_id" : ObjectId("5f9c376a29491ade8d9d3e79"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 32, "salary" : 1600, "deptno" : 30 }

3. 批量更新数据

  • 把年龄是35岁的更新为34岁
  • 设置第三个参数:{multi:true}
  1. > db.emp.update({age:35},{$set:{age:34}},{multi:true})
  2. WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
  • 通过db.emp.find()验证所有数据没有年龄为35的员工

4. $inc使用

  • $inc将一个字段的值增加或者减少
  • 把李元芳的年龄减少1岁,同时薪资加2000
  1. > db.emp.update({ename:"李元芳"},{$inc:{age:-1,salary:2000}})
  2. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  • 验证
  1. > db.emp.find({ename:"李元芳"})
  2. { "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 33, "salary" : 10000, "deptno" : 30 }

七、删除操作

1. 删除指定条件的数据

  1. db.emp.remove({ename:"李元芳"})

2. 删除所有数据

  1. db.emp.remove({})

3. 删除集合

  1. db.emp.drop()

4.删除数据库

  1. db.dropDatabase()

5. 温馨提示

  • 对数据库数据执行删除操作时,记得加条件!

八、MongoDB数据库索引

  • MongoDB数据库索引是指对数据库集合中的一列或者多列进行排序的一种结构,可以大大缩减我们在使用数据库查询时候的时间,其用法和关系型数据库一样。

1. 模拟批量插入数据

  • 在使用数据库前,我们创建一个com数据库和users集合,模拟60万条数据
  • PS:插入60万条数据大概需要5分钟
  1. > use com
  2. switched to db com
  3. > for(var i=0;i<600000;i++){
  4. ... db.users.insert({userNo:i,userName:"张三"+i,age:28,phone:"13000"+i})
  5. ... }
  6. WriteResult({ "nInserted" : 1 })
  • 验证数据
  1. > db.users.find().count()
  2. 600000

2.无索引查询所耗费时间

  1. 命令:
  2. db.users.find({userNo:599999}).explain("executionStats")
  1. > db.users.find({userNo:599999}).explain("executionStats")
  2. {
  3. "queryPlanner" : {
  4. "plannerVersion" : 1,
  5. "namespace" : "com.users",
  6. "indexFilterSet" : false,
  7. "parsedQuery" : {
  8. "userNo" : {
  9. "$eq" : 599999
  10. }
  11. },
  12. "winningPlan" : {
  13. "stage" : "COLLSCAN",
  14. "filter" : {
  15. "userNo" : {
  16. "$eq" : 599999
  17. }
  18. },
  19. "direction" : "forward"
  20. },
  21. "rejectedPlans" : [ ]
  22. },
  23. "executionStats" : {
  24. "executionSuccess" : true,
  25. "nReturned" : 1,
  26. "executionTimeMillis" : 254,
  27. "totalKeysExamined" : 0,
  28. "totalDocsExamined" : 600000,
  29. "executionStages" : {
  30. "stage" : "COLLSCAN",
  31. "filter" : {
  32. "userNo" : {
  33. "$eq" : 599999
  34. }
  35. },
  36. "nReturned" : 1,
  37. "executionTimeMillisEstimate" : 3,
  38. "works" : 600002,
  39. "advanced" : 1,
  40. "needTime" : 600000,
  41. "needYield" : 0,
  42. "saveState" : 600,
  43. "restoreState" : 600,
  44. "isEOF" : 1,
  45. "direction" : "forward",
  46. "docsExamined" : 600000
  47. }
  48. },
  49. "serverInfo" : {
  50. "host" : "thinkPadE580",
  51. "port" : 27017,
  52. "version" : "4.4.2-rc0",
  53. "gitVersion" : "b5fafa1f87dda6f8773c5a8a1a5e7776d4d94da7"
  54. },
  55. "ok" : 1
  56. }
  • 通过"executionTimeMillis" : 254可以知道查询所耗费时间为254毫秒,当然这与计算机配置性能有关。

3. 创建索引

  • 为userNo创建索引
  1. 命令:
  2. db.users.ensureIndex({userNo:1})
  1. > db.users.ensureIndex({userNo:1})
  2. {
  3. "createdCollectionAutomatically" : false,
  4. "numIndexesBefore" : 1,
  5. "numIndexesAfter" : 2,
  6. "ok" : 1
  7. }

4. 查询索引

  1. 命令:
  2. db.users.getIndexes()
  1. > db.users.getIndexes()
  2. [
  3. {
  4. "v" : 2,
  5. "key" : {
  6. "_id" : 1
  7. },
  8. "name" : "_id_"
  9. },
  10. {
  11. "v" : 2,
  12. "key" : {
  13. "userNo" : 1
  14. },
  15. "name" : "userNo_1"
  16. }
  17. ]
  • _id为集合默认id索引,userNo是自定义索引

5. 使用索引查询所耗费时间

  1. > db.users.find({userNo:599999}).explain("executionStats")
  2. {
  3. "queryPlanner" : {
  4. "plannerVersion" : 1,
  5. "namespace" : "com.users",
  6. "indexFilterSet" : false,
  7. "parsedQuery" : {
  8. "userNo" : {
  9. "$eq" : 599999
  10. }
  11. },
  12. "winningPlan" : {
  13. "stage" : "FETCH",
  14. "inputStage" : {
  15. "stage" : "IXSCAN",
  16. "keyPattern" : {
  17. "userNo" : 1
  18. },
  19. "indexName" : "userNo_1",
  20. "isMultiKey" : false,
  21. "multiKeyPaths" : {
  22. "userNo" : [ ]
  23. },
  24. "isUnique" : false,
  25. "isSparse" : false,
  26. "isPartial" : false,
  27. "indexVersion" : 2,
  28. "direction" : "forward",
  29. "indexBounds" : {
  30. "userNo" : [
  31. "[599999.0, 599999.0]"
  32. ]
  33. }
  34. }
  35. },
  36. "rejectedPlans" : [ ]
  37. },
  38. "executionStats" : {
  39. "executionSuccess" : true,
  40. "nReturned" : 1,
  41. "executionTimeMillis" : 84,
  42. "totalKeysExamined" : 1,
  43. "totalDocsExamined" : 1,
  44. "executionStages" : {
  45. "stage" : "FETCH",
  46. "nReturned" : 1,
  47. "executionTimeMillisEstimate" : 0,
  48. "works" : 2,
  49. "advanced" : 1,
  50. "needTime" : 0,
  51. "needYield" : 0,
  52. "saveState" : 0,
  53. "restoreState" : 0,
  54. "isEOF" : 1,
  55. "docsExamined" : 1,
  56. "alreadyHasObj" : 0,
  57. "inputStage" : {
  58. "stage" : "IXSCAN",
  59. "nReturned" : 1,
  60. "executionTimeMillisEstimate" : 0,
  61. "works" : 2,
  62. "advanced" : 1,
  63. "needTime" : 0,
  64. "needYield" : 0,
  65. "saveState" : 0,
  66. "restoreState" : 0,
  67. "isEOF" : 1,
  68. "keyPattern" : {
  69. "userNo" : 1
  70. },
  71. "indexName" : "userNo_1",
  72. "isMultiKey" : false,
  73. "multiKeyPaths" : {
  74. "userNo" : [ ]
  75. },
  76. "isUnique" : false,
  77. "isSparse" : false,
  78. "isPartial" : false,
  79. "indexVersion" : 2,
  80. "direction" : "forward",
  81. "indexBounds" : {
  82. "userNo" : [
  83. "[599999.0, 599999.0]"
  84. ]
  85. },
  86. "keysExamined" : 1,
  87. "seeks" : 1,
  88. "dupsTested" : 0,
  89. "dupsDropped" : 0
  90. }
  91. }
  92. },
  93. "serverInfo" : {
  94. "host" : "thinkPadE580",
  95. "port" : 27017,
  96. "version" : "4.4.2-rc0",
  97. "gitVersion" : "b5fafa1f87dda6f8773c5a8a1a5e7776d4d94da7"
  98. },
  99. "ok" : 1
  100. }
  • 用索引查询userNo的值,可以通过"executionTimeMillisEstimate" : 0知道,所耗费的时间为0毫秒,大大缩短了查询速度

6. 删除索引

  1. 命令:
  2. db.users.dropIndex({userNo:1})

验证

  1. > db.users.dropIndex({userNo:1})
  2. { "nIndexesWas" : 2, "ok" : 1 }
  • 可以看出,我们自定义的索引已经删除

7. 复合索引

  • 当要对多个字段进行经常性大量查询的时候,我们可以设置复合索引
  1. > db.users.ensureIndex({userNo:1,userName:1})
  2. {
  3. "createdCollectionAutomatically" : false,
  4. "numIndexesBefore" : 1,
  5. "numIndexesAfter" : 2,
  6. "ok" : 1
  7. }
  • 查看复合索引
  1. > db.users.getIndexes()
  2. [
  3. {
  4. "v" : 2,
  5. "key" : {
  6. "_id" : 1
  7. },
  8. "name" : "_id_"
  9. },
  10. {
  11. "v" : 2,
  12. "key" : {
  13. "userNo" : 1,
  14. "userName" : 1
  15. },
  16. "name" : "userNo_1_userName_1"
  17. }
  18. ]
  • userNo和userName是我们自定义的索引
  • 查询userName:”张三599999”所耗费时间
  1. > db.users.find({userNo:599999,userName:"张三599999"}).explain("executionStats")))
  2. {
  3. "queryPlanner" : {
  4. "plannerVersion" : 1,
  5. "namespace" : "com.users",
  6. "indexFilterSet" : false,
  7. "parsedQuery" : {
  8. "$and" : [
  9. {
  10. "userName" : {
  11. "$eq" : "张三599999"
  12. }
  13. },
  14. {
  15. "userNo" : {
  16. "$eq" : 599999
  17. }
  18. }
  19. ]
  20. },
  21. "winningPlan" : {
  22. "stage" : "FETCH",
  23. "inputStage" : {
  24. "stage" : "IXSCAN",
  25. "keyPattern" : {
  26. "userNo" : 1,
  27. "userName" : 1
  28. },
  29. "indexName" : "userNo_1_userName_1",
  30. "isMultiKey" : false,
  31. "multiKeyPaths" : {
  32. "userNo" : [ ],
  33. "userName" : [ ]
  34. },
  35. "isUnique" : false,
  36. "isSparse" : false,
  37. "isPartial" : false,
  38. "indexVersion" : 2,
  39. "direction" : "forward",
  40. "indexBounds" : {
  41. "userNo" : [
  42. "[599999.0, 599999.0]"
  43. ],
  44. "userName" : [
  45. "[\"张三599999\", \"张三599999\"]"
  46. ]
  47. }
  48. }
  49. },
  50. "rejectedPlans" : [ ]
  51. },
  52. "executionStats" : {
  53. "executionSuccess" : true,
  54. "nReturned" : 1,
  55. "executionTimeMillis" : 2,
  56. "totalKeysExamined" : 1,
  57. "totalDocsExamined" : 1,
  58. "executionStages" : {
  59. "stage" : "FETCH",
  60. "nReturned" : 1,
  61. "executionTimeMillisEstimate" : 0,
  62. "works" : 2,
  63. "advanced" : 1,
  64. "needTime" : 0,
  65. "needYield" : 0,
  66. "saveState" : 0,
  67. "restoreState" : 0,
  68. "isEOF" : 1,
  69. "docsExamined" : 1,
  70. "alreadyHasObj" : 0,
  71. "inputStage" : {
  72. "stage" : "IXSCAN",
  73. "nReturned" : 1,
  74. "executionTimeMillisEstimate" : 0,
  75. "works" : 2,
  76. "advanced" : 1,
  77. "needTime" : 0,
  78. "needYield" : 0,
  79. "saveState" : 0,
  80. "restoreState" : 0,
  81. "isEOF" : 1,
  82. "keyPattern" : {
  83. "userNo" : 1,
  84. "userName" : 1
  85. },
  86. "indexName" : "userNo_1_userName_1",
  87. "isMultiKey" : false,
  88. "multiKeyPaths" : {
  89. "userNo" : [ ],
  90. "userName" : [ ]
  91. },
  92. "isUnique" : false,
  93. "isSparse" : false,
  94. "isPartial" : false,
  95. "indexVersion" : 2,
  96. "direction" : "forward",
  97. "indexBounds" : {
  98. "userNo" : [
  99. "[599999.0, 599999.0]"
  100. ],
  101. "userName" : [
  102. "[\"张三599999\", \"张三599999\"]"
  103. ]
  104. },
  105. "keysExamined" : 1,
  106. "seeks" : 1,
  107. "dupsTested" : 0,
  108. "dupsDropped" : 0
  109. }
  110. }
  111. },
  112. "serverInfo" : {
  113. "host" : "thinkPadE580",
  114. "port" : 27017,
  115. "version" : "4.4.2-rc0",
  116. "gitVersion" : "b5fafa1f87dda6f8773c5a8a1a5e7776d4d94da7"
  117. },
  118. "ok" : 1
  119. }
  • 通过"executionTimeMillis" : 2,可以看出,查询userNo和userName只需要2毫秒

  • 注意:如果在一个集合中,对多个字段设置索引N(N!=1),在使用复合索引查询的时候,要连同第一个索引字段一起查询,如果只单单查询第N个,索引将没有效果。

8.唯一索引

  • 创建唯一索引的条件是,集合中字段的数据不能重复,但在缺省情况下创建是索引均不是唯一索引

  • 由于集合中的age都是一样的值,在给age创建唯一索引的时候会报错

  1. 命令:
  2. > db.users.ensure({age:1},{unique:true})
  • 为age创建唯一索引,失败
  1. > db.users.ensure({age:1},{unique:true})
  2. TypeError: db.users.ensure is not a function :
  3. @(shell):1:1
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post