MySQL新特性文檔型數據庫


mongodb在文檔型數據庫這方面一直做的很好,也發展了很多年,MySQL作為一個比較大眾的數據庫也慢慢支持了該特性,下面介紹一下MySQL支持文檔型數據庫的簡單操作。

環境:

主機名 IP 系統 軟件
master 192.168.91.46 RHEL MYSQL8.0.17/mysqlsh   Ver 8.0.17

 

[root@master ~]# mysqlsh --mysqlx  -u root  -pkavl7kAkkle! --file /opt/world_x-db/world_x.sql WARNING: Using a password on the command line interface can be insecure.  建立schema

Records: 4079  Duplicates: 0  Warnings: 0

Records: 239  Duplicates: 0  Warnings: 0

Records: 239  Duplicates: 0  Warnings: 0

Records: 984  Duplicates: 0  Warnings: 0

進入庫中:

[root@master ~]# mysqlsh --mysqlx  -u root  -pkavl7kAkkle! --database world_x

MySQL Shell 8.0.17

Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates.

Other names may be trademarks of their respective owners.

Type '\help' or '\?' for help; '\quit' to exit.

WARNING: Using a password on the command line interface can be insecure.

Creating an X protocol session to 'root@localhost/world_x' Fetching schema names for autocompletion... Press ^C to stop.

Your MySQL connection id is 240126 (X protocol)

Server version: 8.0.17 MySQL Community Server - GPL

Default schema `world_x` accessible through db.  

MySQL  localhost:33060+ ssl  world_x  JS >

或者:

[root@master ~]# mysqlsh --mysqlx  -u root  -pkavl7kAkkle!

 MySQL  localhost:33060+ ssl  JS > \use world_x
Default schema `world_x` accessible through db.
 MySQL  localhost:33060+ ssl  world_x  JS >

獲取所有的表

 MySQL  localhost:33060+ ssl  world_x  JS > db.getCollections()
[
    <Collection:countryinfo>
]

查看所有的數據:

 MySQL  localhost:33060+ ssl  world_x  JS > db.countryinfo.find().limit(1)
{
    "GNP": 828,
    "_id": "ABW",
    "Name": "Aruba",
    "IndepYear": null,
    "geography": {
        "Region": "Caribbean",
        "Continent": "North America",
        "SurfaceArea": 193
    },
    "government": {
        "HeadOfState": "Beatrix",
        "GovernmentForm": "Nonmetropolitan Territory of The Netherlands"
    },
    "demographics": {
        "Population": 103000,
        "LifeExpectancy": 78.4000015258789
    }
}
1 document in set (0.0005 sec)
 MySQL  localhost:33060+ ssl  world_x  JS >

統計有多少條數據:

MySQL  localhost:33060+ ssl  world_x  JS > db.countryinfo.count()
239

創建一個表:

 MySQL  localhost:33060+ ssl  world_x  JS > db.createCollection("student_info");
<Collection:student_info>

 MySQL  localhost:33060+ ssl  world_x  JS > db.getCollections()
[
    <Collection:countryinfo>,
    <Collection:student_info>
]

MySQL  localhost:33060+ ssl  world_x  JS > db.student_info.add({name:'thunder',age:27,gender:"man"})
Query OK, 1 item affected (0.0066 sec)

MySQL  localhost:33060+ ssl  world_x  JS > db.student_info.add({aihao:"pingpang",yuyan:"shell"})
Query OK, 1 item affected (0.0076 sec)

MySQL  localhost:33060+ ssl  world_x  JS > db.student_info.add({name:'ss',age:25,gender:"girl"})
Query OK, 1 item affected (0.0072 sec)
 MySQL  localhost:33060+ ssl  world_x  JS > db.student_info.add({name:'aa',age:24,gender:"girl"})
Query OK, 1 item affected (0.0039 sec)
 MySQL  localhost:33060+ ssl  world_x  JS > db.student_info.add({name:'jj',age:27,gender:"boy"})
Query OK, 1 item affected (0.0118 sec)

 MySQL  localhost:33060+ ssl  world_x  JS > db.student_info.find()
{
    "_id": "00005da579b70000000000000001",
    "age": 27,
    "name": "thunder",
    "gender": "man"
}
{
    "_id": "00005da579b70000000000000002",
    "aihao": "pingpang",
    "yuyan": "shell"
}
2 documents in set (0.0005 sec)

按照條件查詢

MySQL  localhost:33060+ ssl  world_x  JS > db.student_info.find("age=27")
{
    "_id": "00005da579b70000000000000001",
    "age": 27,
    "name": "thunder",
    "gender": "man"
}
1 document in set (0.0008 sec)

 MySQL  localhost:33060+ ssl  world_x  JS > db.student_info.find("age>27")
Empty set (0.0007 sec)

刪除文檔的語法結構:

db.集合名稱.remove(條件)
MySQL  localhost:33060+ ssl  world_x  JS > db.student_info.find("age=24")
{
    "_id": "00005da579b70000000000000004",
    "age": 24,
    "name": "qian",
    "gender": "girl"
}
{
    "_id": "00005da579b70000000000000008",
    "age": 24,
    "name": "aa",
    "gender": "girl"
}
2 documents in set (0.0006 sec)

MySQL  localhost:33060+ ssl  world_x  JS > db.student_info.remove("age=24")
Query OK, 2 items affected (0.0044 sec)
 MySQL  localhost:33060+ ssl  world_x  JS > db.student_info.find("age=24")
Empty set (0.0004 sec)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM