[shawnyan@rl8 ~]$ mysql -uroot mytest Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 92 Server version: 9.0.1 MySQL Community Server - GPL
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create table tbl (id int, name varchar(10)); Query OK, 0 rows affected (0.00 sec)
[shawnyan@rl8 ~]$ mysqlsh root@localhost:33060 --js MySQL Shell 9.0.1
Copyright (c) 2016, 2024, Oracle and/or its affiliates. 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. Creating a session to 'root@localhost:33060' Fetching schema names for auto-completion... Press ^C to stop. Your MySQL connection id is 96 (X protocol) Server version: 9.0.1 MySQL Community Server - GPL No default schema selected; type \use <schema> to set one. MySQL localhost:33060+ ssl JS > \use mytest Default schema `mytest` accessible through db. MySQL localhost:33060+ ssl mytest JS > db.getTables(); [ <Table:tbl> ] MySQL localhost:33060+ ssl mytest JS > db.tbl.select(); +----+------+ | id | name | +----+------+ | 1 | Yan | +----+------+ 1 row in set (0.0007 sec) MySQL localhost:33060+ ssl mytest JS > db.tbl.insert("id", "name").values(2, 'Shawn'); Query OK, 1 item affected (0.0016 sec) MySQL localhost:33060+ ssl mytest JS > db.tbl.select().where("id = 2"); +----+-------+ | id | name | +----+-------+ | 2 | Shawn | +----+-------+ 1 row in set (0.0005 sec)
对集合进行操作
创建、查看集合。
1 2 3 4 5 6
MySQL localhost:33060+ ssl mytest JS > db.createCollection('city'); <Collection:city> MySQL localhost:33060+ ssl mytest JS > db.getCollections(); [ <Collection:city> ]
写入、查看数据。
1 2 3 4 5 6 7 8 9
MySQL localhost:33060+ ssl mytest JS > db.city.add({"id": 1, "Name": "Yan"}); Query OK, 1 item affected (0.0018 sec) MySQL localhost:33060+ ssl mytest JS > db.city.find("id = 1"); { "id": 1, "_id": "000066d6dd3d0000000000000002", "Name": "Yan" } 1 document in set (0.0005 sec)
mysql> select doc from city where doc->>"$.id" = 1; +-----------------------------------------------------------------+ | doc | +-----------------------------------------------------------------+ | {"id": 1, "_id": "000066d6dd3d0000000000000002", "Name": "Yan"} | +-----------------------------------------------------------------+ 1 row in set (0.00 sec)
mysql> update city set doc = json_replace(doc, '$."Name"', 'Yan1') where doc->>"$.id" = 1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
mysql> select json_extract(doc, '$.id') as id, json_extract(doc, '$.Name') as name from city; +------+--------+ | id | name | +------+--------+ | 1 | "Yan1" | +------+--------+ 1 row in set (0.00 sec)
請注意,除了新增文件時指定的字段外,還有一個字段,即_id。每個文檔都需要一個名為 _id 的標識符欄位。 _id 欄位的值在同一集合中的所有文件中必須是唯一的。文檔 ID 由伺服器而非客戶端生成,因此 MySQL Shell 不會自動設定 _id 值。如果文檔不包含 _id 字段,MySQL 將設定 _id 值。