Rumah > pangkalan data > MongoDB > Tutorial asas MongoDB 4.X

Tutorial asas MongoDB 4.X

P粉469731340
Lepaskan: 2022-07-20 09:47:22
asal
1650 orang telah melayarinya

1. Pengenalan kepada MongoDB

  • MongoDB ialah pangkalan data berdasarkan storan fail yang diedarkan.

  • ditulis dalam bahasa C. Direka bentuk untuk menyediakan penyelesaian storan data berprestasi tinggi berskala untuk aplikasi WEB.

  • MongoDB ialah produk antara pangkalan data hubungan dan pangkalan data bukan perhubungan Ia adalah pangkalan data yang paling kaya dengan ciri dan hampir sama dengan a pangkalan data hubungan.

  • Struktur data yang disokong oleh MongoDB sangat longgar dan merupakan format BJSON yang serupa dengan JSON, jadi ia boleh menyimpan jenis data yang lebih kompleks. Ciri terbesar Mongo ialah bahasa pertanyaan yang disokongnya sangat berkuasa Sintaksnya agak serupa dengan bahasa pertanyaan berorientasikan objek Ia hampir boleh melaksanakan kebanyakan fungsi yang serupa dengan pertanyaan jadual tunggal dalam pangkalan data hubungan, dan ia juga menyokong pengindeksan. daripada data.

  • Operasi data MongoDB adalah berdasarkan format json

<span style="font-size: 16px;"> { "userName":"admin", "password":123456 }<br/></span>
Salin selepas log masuk

2. Pemasangan MongoDB

1. Muat turun MongoDB

  • Tapak web: https :/ /www.mongodb.com/try/download/community

    Tutorial asas MongoDB 4.X

  • Gambar di atas adalah berdasarkan apabila memilih versi Pilih sistem anda sendiri, termasuk Windows, LInux, CentOS, Ubuntu, dll. untuk dipilih.

2.Pemasangan MongoDB

  • Fail .msi dimuat turun , klik dua kali fail selepas memuat turun dan ikut arahan untuk memasangnya.

  • Semasa proses pemasangan, anda boleh menetapkan direktori pemasangan anda dengan mengklik butang "Custom" Anda disyorkan untuk tidak memasang pada pemacu C.

Tutorial asas MongoDB 4.X

Klik sebelah untuk memasang sepanjang proses, tetapi anda perlu memberi perhatian kepada langkah ini . Berikut ialah pemasangan komponen visual yang lalai ialah keadaan yang dipilih Anda perlu menyahtandanya di sini, jika tidak, komponen visual akan dimuat turun semasa proses pemasangan, yang agak perlahan dan kadangkala ralat akan dilaporkan:

Tutorial asas MongoDB 4.X

3. >Klik kanan komputer ini pada desktop>>>Properties> ;>>Tetapan Sistem Terperinci>>>Terperinci>>>Pembolehubah Persekitaran>>>Cari> ;Pilih Edit>>>Baharu

Tutorial asas MongoDB 4.X Salin laluan bin pemasangan MongoDB dalam pembolehubah persekitaran yang dibuka ke direktori yang baru dibuat

Tutorial asas MongoDB 4.X Kemudian klik OK untuk melengkapkan konfigurasi pembolehubah persekitaran

  • 4 Sahkan pemasangan berjaya

Buka CMD Dalam tetingkap arahan, masukkan mongo Mesej gesaan berikut muncul, menunjukkan bahawa pemasangan berjaya.

  • 3 Pengenalan asas untuk menggunakan MongoDB
<span style="font-size: 16px;">C:\Users\***.DESKTOP-C1RC9P2>mongo<br/>MongoDB shell version v4.4.2-rc0<br/>connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb<br/>Implicit session: session { "id" : UUID("df31999e-cb62-4f71-8a18-7db8723c514f") }<br/>MongoDB server version: 4.4.2-rc0<br/>---<br/>The server generated these startup warnings when booting:<br/>        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<br/>---<br/>---<br/>        Enable MongoDB&#39;s free cloud-based monitoring service, which will then receive and display<br/>        metrics about your deployment (disk utilization, CPU, operation statistics, etc).<br/><br/>        The monitoring data will be available on a MongoDB website with a unique URL accessible to you<br/>        and anyone you share the URL with. MongoDB may use this information to make product<br/>        improvements and to suggest MongoDB products and deployment options to you.<br/><br/>        To enable free monitoring, run the following command: db.enableFreeMonitoring()<br/>        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()<br/></span>
Salin selepas log masuk

<. 🎜 >MongoDB ialah pangkalan data bukan perhubungan, dan pangkalan datanya, jadual, medan, dll. agak berbeza daripada pangkalan data hubungan (seperti pangkalan data MySQL); >MongoDB Koleksi adalah bersamaan dengan jadual dalam pangkalan data relasi Kunci rentetan json dalam MongoDB adalah bersamaan dengan nama lajur dalam pangkalan data relasi; > semasa mengendalikan MongoDB Semua data menggunakan format data json.

  • 1 Lihat nama pangkalan data

    • 查看所有数据库名

    <span style="font-size: 16px;">命令:<br/>  show dbs<br/></span>
    Salin selepas log masuk
    <span style="font-size: 16px;">  > show dbs<br/>  admin   0.000GB<br/>  config  0.000GB<br/>  local   0.000GB<br/></span>
    Salin selepas log masuk
    • 这三个数据库是默认系统数据库,不能删除。

    2. 查看集合

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

    <span style="font-size: 16px;">命令:<br/>ues 数据库名 show collections<br/></span>
    Salin selepas log masuk
    <span style="font-size: 16px;">> use admin <br/>switched to db admin <br/>> show collections <br/>system.version<br/></span>
    Salin selepas log masuk
    • system.version 就是admin这个数据库中的表

    3.查询集合中的数据

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

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

    <span style="font-size: 16px;">命令:<br/>	db.集合名.find()<br/></span>
    Salin selepas log masuk
    <span style="font-size: 16px;">> db.system.version.find()<br/>{ "_id" : "featureCompatibilityVersion", "version" : "4.4" }<br/></span>
    Salin selepas log masuk
    • 这里的_id是集合的键,每个集合里面默认存在,version是集合中的另一个键,相当于关系型 数据库中的字段

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

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

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

    • 先指定创建的数据

    • 然后执行添加数据命令

    <span style="font-size: 16px;">> use company<br/>switched to db company<br/>> db.emp.insert({"empno":100,"ename":"admin","sex":"男","age":20,"salary":800.00,"deptno":10})<br/>WriteResult({ "nInserted" : 1 })<br/></span>
    Salin selepas log masuk
    • 这里插入了6列数据,分别是员工的编号,姓名,性别,年龄,薪资及所在的部门

    <span style="font-size: 16px;">WriteResult({ "nInserted" : 1 })<br/></span>
    Salin selepas log masuk
    • 表示一行数据插入成功,说明创建数据成功,同时添加 了一套数据,我们可以继续添加

    <span style="font-size: 16px;">>  db.emp.insert({"empno":101,"ename":"张三","sex":"女","age":30,"salary":2500.00,"deptno":20})})})})<br/>WriteResult({ "nInserted" : 1 })</span>
    Salin selepas log masuk
    • 随着数据的插入,数据库也随着创建成功。

    五、查询数据

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

    1.查询所有数据

    <span style="font-size: 16px;">命令:<br/>db.集合名.find() </span>
    Salin selepas log masuk
    <span style="font-size: 16px;">指定对哪个数据库操作<br/>	> use company<br/>	switched to db company<br/>查询指定数据库后里面的所有集合<br/>	> show collections<br/>emp<br/>查询所有数据,一共12条数据:<br/>	> db.emp.find()<br/><br/>{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 34, "salary" : 12000, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 34, "salary" : 12000, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 33, "salary" : 10000, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c371b5df291fa8194b927"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c376a29491ade8d9d3e79"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 32, "salary" : 1600, "deptno" : 30 }<br/></span>
    Salin selepas log masuk

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

    <span style="font-size: 16px;">命令:<br/>> db.集合名.distinct("ename")<br/></span>
    Salin selepas log masuk
    <span style="font-size: 16px;">> db.emp.distinct("ename")<br/><br/>[<br/>        "admin",<br/>        "刘静",<br/>        "张三",<br/>        "张四",<br/>        "张良",<br/>        "李元芳",<br/>        "李元静",<br/>        "李四",<br/>        "李明",<br/>        "李菲菲",<br/>        "王三"<br/>]<br/></span>
    Salin selepas log masuk
    • 上面的结果“李四”重复被去掉了

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

    • 这里的“age”可以不加“ ”,直接写成{age:25}

    <span style="font-size: 16px;">> db.emp.find({"age":25})<br/><br/>{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }<br/></span>
    Salin selepas log masuk

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

    <span style="font-size: 16px;">> db.emp.find({"ename":"李元芳"})<br/><br/>{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }<br/></span>
    Salin selepas log masuk

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

    <span style="font-size: 16px;">> db.emp.find({"age":{$gt:30}})<br/><br/>{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }<br/></span>
    Salin selepas log masuk

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

    <span style="font-size: 16px;">> db.emp.find({"age":{$lt:25}})<br/><br/>{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }<br/></span>
    Salin selepas log masuk

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

    <span style="font-size: 16px;">> db.emp.find({"age":{$gte:30}})<br/><br/>{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }<br/></span>
    Salin selepas log masuk

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

    <span style="font-size: 16px;">> db.emp.find({"age":{$lte:30}})<br/><br/>{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }<br/></span>
    Salin selepas log masuk

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

    <span style="font-size: 16px;">> db.emp.find({"age":{$gte:25,$lte:30}})<br/><br/>{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }<br/></span>
    Salin selepas log masuk

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

    • 模糊查询一般用于查询数据量比较小的集合数据

    <span style="font-size: 16px;">> db.emp.find({"ename":/李/})<br/><br/>{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }<br/></span>
    Salin selepas log masuk

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

    <span style="font-size: 16px;">> db.emp.find({"ename":/^张/})<br/><br/>{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }<br/></span>
    Salin selepas log masuk

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

    <span style="font-size: 16px;">> db.emp.find({"ename":/静$/})<br/><br/>{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }<br/>{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }<br/></span>
    Salin selepas log masuk

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

    • 查询所有员工年龄的数据

    <span style="font-size: 16px;">> db.emp.find({},{"age":1})<br/><br/>{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "age" : 20 }<br/>{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "age" : 30 }<br/>{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "age" : 25 }<br/>{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "age" : 30 }<br/>{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "age" : 28 }<br/>{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "age" : 32 }<br/>{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "age" : 35 }<br/>{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "age" : 35 }<br/>{ "_id" : ObjectId("5f9c21d85df291fa8194b923"), "age" : 27 }<br/>{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "age" : 35 }<br/>{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "age" : 35 }<br/>{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "age" : 25 }<br/></span>
    Salin selepas log masuk
    • 查询所有员工的姓名和年龄的数据

    <span style="font-size: 16px;">> db.emp.find({},{"ename":1,"age":1})<br/><br/>{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "ename" : "admin", "age" : 20 }<br/>{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "ename" : "张三", "age" : 30 }<br/>{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "ename" : "张良", "age" : 25 }<br/>{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "ename" : "李明", "age" : 30 }<br/>{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "ename" : "李菲菲", "age" : 28 }<br/>{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "ename" : "张四", "age" : 32 }<br/>{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "ename" : "李四", "age" : 35 }<br/>{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "ename" : "李四", "age" : 35 }<br/>{ "_id" : ObjectId("5f9c21d85df291fa8194b923"), "ename" : "王三", "age" : 27 }<br/>{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "ename" : "李元芳", "age" : 35 }<br/>{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "ename" : "李元静", "age" : 35 }<br/>{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "ename" : "刘静", "age" : 25 }<br/></span>
    Salin selepas log masuk
    • 查询age>=30的员工姓名、年龄和薪资

    <span style="font-size: 16px;">> db.emp.find({"age":{$gte:30}},{"ename":1,"age":1,"salary":1})<br/><br/>{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "ename" : "张三", "age" : 30, "salary" : 2500 }<br/>{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "ename" : "李明", "age" : 30, "salary" : 1800 }<br/>{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "ename" : "张四", "age" : 32, "salary" : 8000 }<br/>{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "ename" : "李四", "age" : 35, "salary" : 12000 }<br/>{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "ename" : "李四", "age" : 35, "salary" : 12000 }<br/>{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "ename" : "李元芳", "age" : 35, "salary" : 8000 }<br/>{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "ename" : "李元静", "age" : 35, "salary" : 15000 }<br/></span>
    Salin selepas log masuk

    14. 排序

    • 按照年龄升序排列

    <span style="font-size: 16px;">> db.emp.find().sort({"age":1})<br/><br/>{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }<br/></span>
    Salin selepas log masuk
    • 按照薪资降序排列

    <span style="font-size: 16px;">> db.emp.find().sort({salary:-1})<br/><br/>{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }<br/>{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }<br/></span>
    Salin selepas log masuk

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

    <span style="font-size: 16px;">> db.emp.find({ename:"admin",age:20})<br/><br/>{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }<br/></span>
    Salin selepas log masuk

    16. 查询前5条数据

    <span style="font-size: 16px;">> db.emp.find().limit(5)<br/><br/>{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }<br/></span>
    Salin selepas log masuk

    17. 查询10条以后的数据

    • 这里一共12条数据,查询10条以后的数据,结果是两条数据。

    <span style="font-size: 16px;">> db.emp.find().skip(10)<br/><br/>{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }<br/>{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }<br/></span>
    Salin selepas log masuk

    18. 查询集合中的总数据

    • 一共12条数据

    <span style="font-size: 16px;">> db.emp.find().count()<br/><br/>12<br/></span>
    Salin selepas log masuk
    • 查询salary>=5000的员工数量

    <span style="font-size: 16px;">> db.emp.find({salary:{$gte:5000}}).count()<br/><br/>6<br/></span>
    Salin selepas log masuk

    验证(查询查询salary>=5000的员工数据,一共是6条。)

    <span style="font-size: 16px;">> db.emp.find({salary:{$gte:5000}})<br/><br/>{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }<br/></span>
    Salin selepas log masuk

    19. 分页查询

    • 按照每页显示5条数据查询,一共12条数据,就要查询3页

    <span style="font-size: 16px;">第一页:<br/>> db.emp.find().skip(0).limit(5)<br/><br/>{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c20945df291fa8194b91d"), "empno" : 102, "ename" : "张良", "sex" : "男", "age" : 25, "salary" : 3000, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c215f5df291fa8194b91f"), "empno" : 104, "ename" : "李菲菲", "sex" : "女", "age" : 28, "salary" : 4200, "deptno" : 20 }<br/><br/>第二页:<br/>> db.emp.find().skip(5).limit(5)<br/><br/>{ "_id" : ObjectId("5f9c21855df291fa8194b920"), "empno" : 105, "ename" : "张四", "sex" : "男", "age" : 32, "salary" : 8000, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c21d85df291fa8194b923"), "empno" : 107, "ename" : "王三", "sex" : "女", "age" : 27, "salary" : 5000, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }<br/><br/>第三页:<br/>> db.emp.find().skip(10).limit(5)<br/><br/>{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }<br/>{ "_id" : ObjectId("5f9c227d5df291fa8194b926"), "empno" : 110, "ename" : "刘静", "sex" : "女", "age" : 25, "salary" : 3500, "deptno" : 10 }<br/></span>
    Salin selepas log masuk
    • skip的值=(页数-1)* 每页显示数量

    • skip(N):表示要查询第N条数据后的数据

    20.关键字or的查询方式

    • 查询年龄是30或者年龄是35的员工数据(注意写法)

    <span style="font-size: 16px;">> db.emp.find({$or:[{age:30},{age:35}]})<br/><br/>{ "_id" : ObjectId("5f9c1dfc5df291fa8194b91c"), "empno" : 101, "ename" : "张三", "sex" : "女", "age" : 30, "salary" : 2500, "deptno" : 20 }<br/>{ "_id" : ObjectId("5f9c21055df291fa8194b91e"), "empno" : 103, "ename" : "李明", "sex" : "女", "age" : 30, "salary" : 1800, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c219b5df291fa8194b921"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c21a75df291fa8194b922"), "empno" : 106, "ename" : "李四", "sex" : "男", "age" : 35, "salary" : 12000, "deptno" : 10 }<br/>{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 35, "salary" : 8000, "deptno" : 30 }<br/>{ "_id" : ObjectId("5f9c22445df291fa8194b925"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 35, "salary" : 15000, "deptno" : 36 }<br/></span>
    Salin selepas log masuk

    21. 查询第一条数据

    <span style="font-size: 16px;">> db.emp.findOne()<br/>{<br/>        "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"),<br/>        "empno" : 100,<br/>        "ename" : "admin",<br/>        "sex" : "男",<br/>        "age" : 20,<br/>        "salary" : 800,<br/>        "deptno" : 10<br/>}<br/></span>
    Salin selepas log masuk


    <span style="font-size: 16px;">> db.emp.find().limit(1)<br/><br/>{ "_id" : ObjectId("5f9c1c5b5df291fa8194b91b"), "empno" : 100, "ename" : "admin", "sex" : "男", "age" : 20, "salary" : 800, "deptno" : 10 }<br/></span>
    Salin selepas log masuk

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

    <span style="font-size: 16px;">> db.emp.getDB()<br/><br/>company<br/></span>
    Salin selepas log masuk

    六、更新数据

    • 更新数据一定要有条件限制,并且需要加上$set否则会全部修改

    • 第一个参数是条件,后面的参数是要修改的数据或者是其他操作,比如批量操作

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

    <span style="font-size: 16px;">> db.emp.update({ename:"李元静"},{$set:{age:32}})<br/><br/>WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })<br/></span>
    Salin selepas log masuk

    验证(查询姓名是李元静的员工数据)

    <span style="font-size: 16px;">> db.emp.find({ename:"李元静"})<br/><br/>{ "_id" : ObjectId("5f9c376a29491ade8d9d3e79"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 32, "salary" : 15000, "deptno" : 36 }<br/></span>
    Salin selepas log masuk

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

    <span style="font-size: 16px;">> db.emp.update({ename:"李元静"},{$set:{salary:1600,deptno:30}})<br/><br/>WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })<br/></span>
    Salin selepas log masuk
    • 验证(查询姓名是李元静的员工数据)

    <span style="font-size: 16px;">> db.emp.find({ename:"李元静"})<br/><br/>{ "_id" : ObjectId("5f9c376a29491ade8d9d3e79"), "empno" : 109, "ename" : "李元静", "sex" : "女", "age" : 32, "salary" : 1600, "deptno" : 30 }<br/></span>
    Salin selepas log masuk

    3. 批量更新数据

    • 把年龄是35岁的更新为34岁

    • 设置第三个参数:{multi:true}

    <span style="font-size: 16px;">> db.emp.update({age:35},{$set:{age:34}},{multi:true})<br/><br/>WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })<br/></span>
    Salin selepas log masuk
    • 通过db.emp.find()验证所有数据没有年龄为35的员工

    4. $inc使用

    • $inc将一个字段的值增加或者减少

    • 把李元芳的年龄减少1岁,同时薪资加2000

    <span style="font-size: 16px;">> db.emp.update({ename:"李元芳"},{$inc:{age:-1,salary:2000}})<br/><br/>WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })<br/></span>
    Salin selepas log masuk
    • 验证

    <span style="font-size: 16px;">> db.emp.find({ename:"李元芳"})<br/><br/>{ "_id" : ObjectId("5f9c22185df291fa8194b924"), "empno" : 108, "ename" : "李元芳", "sex" : "男", "age" : 33, "salary" : 10000, "deptno" : 30 }<br/></span>
    Salin selepas log masuk

    七、删除操作

    1. 删除指定条件的数据

    <span style="font-size: 16px;">db.emp.remove({ename:"李元芳"})<br/></span>
    Salin selepas log masuk

    2. 删除所有数据

    <span style="font-size: 16px;">db.emp.remove({})<br/></span>
    Salin selepas log masuk

    3. 删除集合

    <span style="font-size: 16px;">db.emp.drop()<br/></span>
    Salin selepas log masuk

    4.删除数据库

    <span style="font-size: 16px;">db.dropDatabase()<br/></span>
    Salin selepas log masuk

    5. 温馨提示

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

    八、MongoDB数据库索引

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

    1. 模拟批量插入数据

    • 在使用数据库前,我们创建一个com数据库和users集合,模拟60万条数据

    • PS:插入60万条数据大概需要5分钟

    <span style="font-size: 16px;">> use com<br/>switched to db com<br/>> for(var i=0;i<600000;i++){<br/>... db.users.insert({userNo:i,userName:"张三"+i,age:28,phone:"13000"+i})<br/>... }<br/>WriteResult({ "nInserted" : 1 })<br/></span>
    Salin selepas log masuk
    • 验证数据

    <span style="font-size: 16px;">> db.users.find().count()<br/>600000<br/></span>
    Salin selepas log masuk

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

    <span style="font-size: 16px;">命令: <br/>db.users.find({userNo:599999}).explain("executionStats")<br/></span>
    Salin selepas log masuk
    <span style="font-size: 16px;">> db.users.find({userNo:599999}).explain("executionStats")<br/>{<br/>        "queryPlanner" : {<br/>                "plannerVersion" : 1,<br/>                "namespace" : "com.users",<br/>                "indexFilterSet" : false,<br/>                "parsedQuery" : {<br/>                        "userNo" : {<br/>                                "$eq" : 599999<br/>                        }<br/>                },<br/>                "winningPlan" : {<br/>                        "stage" : "COLLSCAN",<br/>                        "filter" : {<br/>                                "userNo" : {<br/>                                        "$eq" : 599999<br/>                                }<br/>                        },<br/>                        "direction" : "forward"<br/>                },<br/>                "rejectedPlans" : [ ]<br/>        },<br/>        "executionStats" : {<br/>                "executionSuccess" : true,<br/>                "nReturned" : 1,<br/>                "executionTimeMillis" : 254,<br/>                "totalKeysExamined" : 0,<br/>                "totalDocsExamined" : 600000,<br/>                "executionStages" : {<br/>                        "stage" : "COLLSCAN",<br/>                        "filter" : {<br/>                                "userNo" : {<br/>                                        "$eq" : 599999<br/>                                }<br/>                        },<br/>                        "nReturned" : 1,<br/>                        "executionTimeMillisEstimate" : 3,<br/>                        "works" : 600002,<br/>                        "advanced" : 1,<br/>                        "needTime" : 600000,<br/>                        "needYield" : 0,<br/>                        "saveState" : 600,<br/>                        "restoreState" : 600,<br/>                        "isEOF" : 1,<br/>                        "direction" : "forward",<br/>                        "docsExamined" : 600000<br/>                }<br/>        },<br/>        "serverInfo" : {<br/>                "host" : "thinkPadE580",<br/>                "port" : 27017,<br/>                "version" : "4.4.2-rc0",<br/>                "gitVersion" : "b5fafa1f87dda6f8773c5a8a1a5e7776d4d94da7"<br/>        },<br/>        "ok" : 1<br/>}<br/></span>
    Salin selepas log masuk
    • 通过"executionTimeMillis" : 254可以知道查询所耗费时间为254毫秒,当然这与计算机配置性能有关。

    3. 创建索引

    • 为userNo创建索引

    <span style="font-size: 16px;">命令:<br/>	db.users.ensureIndex({userNo:1})<br/></span>
    Salin selepas log masuk
    <span style="font-size: 16px;">> db.users.ensureIndex({userNo:1})<br/>{<br/>        "createdCollectionAutomatically" : false,<br/>        "numIndexesBefore" : 1,<br/>        "numIndexesAfter" : 2,<br/>        "ok" : 1<br/>}<br/></span>
    Salin selepas log masuk

    4. 查询索引

    <span style="font-size: 16px;">命令:<br/>	db.users.getIndexes()<br/></span>
    Salin selepas log masuk
    <span style="font-size: 16px;">> db.users.getIndexes()<br/>[<br/>        {<br/>                "v" : 2,<br/>                "key" : {<br/>                        "_id" : 1<br/>                },<br/>                "name" : "_id_"<br/>        },<br/>        {<br/>                "v" : 2,<br/>                "key" : {<br/>                        "userNo" : 1<br/>                },<br/>                "name" : "userNo_1"<br/>        }<br/>]<br/></span>
    Salin selepas log masuk
    • _id为集合默认id索引,userNo是自定义索引

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

    <span style="font-size: 16px;">> db.users.find({userNo:599999}).explain("executionStats")<br/>{<br/>        "queryPlanner" : {<br/>                "plannerVersion" : 1,<br/>                "namespace" : "com.users",<br/>                "indexFilterSet" : false,<br/>                "parsedQuery" : {<br/>                        "userNo" : {<br/>                                "$eq" : 599999<br/>                        }<br/>                },<br/>                "winningPlan" : {<br/>                        "stage" : "FETCH",<br/>                        "inputStage" : {<br/>                                "stage" : "IXSCAN",<br/>                                "keyPattern" : {<br/>                                        "userNo" : 1<br/>                                },<br/>                                "indexName" : "userNo_1",<br/>                                "isMultiKey" : false,<br/>                                "multiKeyPaths" : {<br/>                                        "userNo" : [ ]<br/>                                },<br/>                                "isUnique" : false,<br/>                                "isSparse" : false,<br/>                                "isPartial" : false,<br/>                                "indexVersion" : 2,<br/>                                "direction" : "forward",<br/>                                "indexBounds" : {<br/>                                        "userNo" : [<br/>                                                "[599999.0, 599999.0]"<br/>                                        ]<br/>                                }<br/>                        }<br/>                },<br/>                "rejectedPlans" : [ ]<br/>        },<br/>        "executionStats" : {<br/>                "executionSuccess" : true,<br/>                "nReturned" : 1,<br/>                "executionTimeMillis" : 84,<br/>                "totalKeysExamined" : 1,<br/>                "totalDocsExamined" : 1,<br/>                "executionStages" : {<br/>                        "stage" : "FETCH",<br/>                        "nReturned" : 1,<br/>                        "executionTimeMillisEstimate" : 0,<br/>                        "works" : 2,<br/>                        "advanced" : 1,<br/>                        "needTime" : 0,<br/>                        "needYield" : 0,<br/>                        "saveState" : 0,<br/>                        "restoreState" : 0,<br/>                        "isEOF" : 1,<br/>                        "docsExamined" : 1,<br/>                        "alreadyHasObj" : 0,<br/>                        "inputStage" : {<br/>                                "stage" : "IXSCAN",<br/>                                "nReturned" : 1,<br/>                                "executionTimeMillisEstimate" : 0,<br/>                                "works" : 2,<br/>                                "advanced" : 1,<br/>                                "needTime" : 0,<br/>                                "needYield" : 0,<br/>                                "saveState" : 0,<br/>                                "restoreState" : 0,<br/>                                "isEOF" : 1,<br/>                                "keyPattern" : {<br/>                                        "userNo" : 1<br/>                                },<br/>                                "indexName" : "userNo_1",<br/>                                "isMultiKey" : false,<br/>                                "multiKeyPaths" : {<br/>                                        "userNo" : [ ]<br/>                                },<br/>                                "isUnique" : false,<br/>                                "isSparse" : false,<br/>                                "isPartial" : false,<br/>                                "indexVersion" : 2,<br/>                                "direction" : "forward",<br/>                                "indexBounds" : {<br/>                                        "userNo" : [<br/>                                                "[599999.0, 599999.0]"<br/>                                        ]<br/>                                },<br/>                                "keysExamined" : 1,<br/>                                "seeks" : 1,<br/>                                "dupsTested" : 0,<br/>                                "dupsDropped" : 0<br/>                        }<br/>                }<br/>        },<br/>        "serverInfo" : {<br/>                "host" : "thinkPadE580",<br/>                "port" : 27017,<br/>                "version" : "4.4.2-rc0",<br/>                "gitVersion" : "b5fafa1f87dda6f8773c5a8a1a5e7776d4d94da7"<br/>        },<br/>        "ok" : 1<br/>}<br/></span>
    Salin selepas log masuk
    • 用索引查询userNo的值,可以通过"executionTimeMillisEstimate" : 0知道,所耗费的时间为0毫秒,大大缩短了查询速度

    6. 删除索引

    <span style="font-size: 16px;">命令:<br/>	 db.users.dropIndex({userNo:1})<br/></span>
    Salin selepas log masuk

    验证

    <span style="font-size: 16px;">> db.users.dropIndex({userNo:1})<br/>{ "nIndexesWas" : 2, "ok" : 1 }<br/></span>
    Salin selepas log masuk
    • 可以看出,我们自定义的索引已经删除

    7. 复合索引

    • 当要对多个字段进行经常性大量查询的时候,我们可以设置复合索引

    <span style="font-size: 16px;">> db.users.ensureIndex({userNo:1,userName:1})<br/>{<br/>        "createdCollectionAutomatically" : false,<br/>        "numIndexesBefore" : 1,<br/>        "numIndexesAfter" : 2,<br/>        "ok" : 1<br/>}<br/></span>
    Salin selepas log masuk
    • 查看复合索引

    <span style="font-size: 16px;">> db.users.getIndexes()<br/>[<br/>        {<br/>                "v" : 2,<br/>                "key" : {<br/>                        "_id" : 1<br/>                },<br/>                "name" : "_id_"<br/>        },<br/>        {<br/>                "v" : 2,<br/>                "key" : {<br/>                        "userNo" : 1,<br/>                        "userName" : 1<br/>                },<br/>                "name" : "userNo_1_userName_1"<br/>        }<br/>]<br/></span>
    Salin selepas log masuk
    • userNo和userName是我们自定义的索引

    • 查询userName:”张三599999”所耗费时间

    <span style="font-size: 16px;">> db.users.find({userNo:599999,userName:"张三599999"}).explain("executionStats")))<br/>{<br/>        "queryPlanner" : {<br/>                "plannerVersion" : 1,<br/>                "namespace" : "com.users",<br/>                "indexFilterSet" : false,<br/>                "parsedQuery" : {<br/>                        "$and" : [<br/>                                {<br/>                                        "userName" : {<br/>                                                "$eq" : "张三599999"<br/>                                        }<br/>                                },<br/>                                {<br/>                                        "userNo" : {<br/>                                                "$eq" : 599999<br/>                                        }<br/>                                }<br/>                        ]<br/>                },<br/>                "winningPlan" : {<br/>                        "stage" : "FETCH",<br/>                        "inputStage" : {<br/>                                "stage" : "IXSCAN",<br/>                                "keyPattern" : {<br/>                                        "userNo" : 1,<br/>                                        "userName" : 1<br/>                                },<br/>                                "indexName" : "userNo_1_userName_1",<br/>                                "isMultiKey" : false,<br/>                                "multiKeyPaths" : {<br/>                                        "userNo" : [ ],<br/>                                        "userName" : [ ]<br/>                                },<br/>                                "isUnique" : false,<br/>                                "isSparse" : false,<br/>                                "isPartial" : false,<br/>                                "indexVersion" : 2,<br/>                                "direction" : "forward",<br/>                                "indexBounds" : {<br/>                                        "userNo" : [<br/>                                                "[599999.0, 599999.0]"<br/>                                        ],<br/>                                        "userName" : [<br/>                                                "[\"张三599999\", \"张三599999\"]"<br/>                                        ]<br/>                                }<br/>                        }<br/>                },<br/>                "rejectedPlans" : [ ]<br/>        },<br/>        "executionStats" : {<br/>                "executionSuccess" : true,<br/>                "nReturned" : 1,<br/>                "executionTimeMillis" : 2,<br/>                "totalKeysExamined" : 1,<br/>                "totalDocsExamined" : 1,<br/>                "executionStages" : {<br/>                        "stage" : "FETCH",<br/>                        "nReturned" : 1,<br/>                        "executionTimeMillisEstimate" : 0,<br/>                        "works" : 2,<br/>                        "advanced" : 1,<br/>                        "needTime" : 0,<br/>                        "needYield" : 0,<br/>                        "saveState" : 0,<br/>                        "restoreState" : 0,<br/>                        "isEOF" : 1,<br/>                        "docsExamined" : 1,<br/>                        "alreadyHasObj" : 0,<br/>                        "inputStage" : {<br/>                                "stage" : "IXSCAN",<br/>                                "nReturned" : 1,<br/>                                "executionTimeMillisEstimate" : 0,<br/>                                "works" : 2,<br/>                                "advanced" : 1,<br/>                                "needTime" : 0,<br/>                                "needYield" : 0,<br/>                                "saveState" : 0,<br/>                                "restoreState" : 0,<br/>                                "isEOF" : 1,<br/>                                "keyPattern" : {<br/>                                        "userNo" : 1,<br/>                                        "userName" : 1<br/>                                },<br/>                                "indexName" : "userNo_1_userName_1",<br/>                                "isMultiKey" : false,<br/>                                "multiKeyPaths" : {<br/>                                        "userNo" : [ ],<br/>                                        "userName" : [ ]<br/>                                },<br/>                                "isUnique" : false,<br/>                                "isSparse" : false,<br/>                                "isPartial" : false,<br/>                                "indexVersion" : 2,<br/>                                "direction" : "forward",<br/>                                "indexBounds" : {<br/>                                        "userNo" : [<br/>                                                "[599999.0, 599999.0]"<br/>                                        ],<br/>                                        "userName" : [<br/>                                                "[\"张三599999\", \"张三599999\"]"<br/>                                        ]<br/>                                },<br/>                                "keysExamined" : 1,<br/>                                "seeks" : 1,<br/>                                "dupsTested" : 0,<br/>                                "dupsDropped" : 0<br/>                        }<br/>                }<br/>        },<br/>        "serverInfo" : {<br/>                "host" : "thinkPadE580",<br/>                "port" : 27017,<br/>                "version" : "4.4.2-rc0",<br/>                "gitVersion" : "b5fafa1f87dda6f8773c5a8a1a5e7776d4d94da7"<br/>        },<br/>        "ok" : 1<br/>}<br/></span>
    Salin selepas log masuk
    • 通过"executionTimeMillis" : 2,可以看出,查询userNo和userName只需要2毫秒

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

    8.唯一索引

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

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

    <span style="font-size: 16px;">命令:<br/>	> db.users.ensure({age:1},{unique:true})<br/></span>
    Salin selepas log masuk
    • 为age创建唯一索引,失败

    <span style="font-size: 16px;">> db.users.ensure({age:1},{unique:true})<br/>TypeError: db.users.ensure is not a function :<br/>@(shell):1:1<br/></span>
    Salin selepas log masuk

    Atas ialah kandungan terperinci Tutorial asas MongoDB 4.X. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan