original 2016-07-12 Du Yishu Performance and architecture
Once logged in to MySQL's console, use the SQL language to manipulate the database, as
Mysql> SELECT * FROM TableName;
Mysql 5.7.12 After the big changes, supporting the operation of the JSON document, but also provides a new way of database operation, now can not need the knowledge of SQL, in a very natural code to manipulate the database, for example
Db.news.insert ("Create_date", "title")
. VALUES ("2016-04-10", "Hello");
You can log in to the shell console and have a new version of the driver that you can use in your code
The shell console has a JavaScript version, so let's experience a new way of doing it
Log in to the shell console
Mysqlsh-u Root world_x
World_x is the database name and is a test library that is officially provided
Mysql-js>
This hint indicates that the current interactive language is Javascript
Because MySQL supports JSON documents, the objects in action add a collection of documents in addition to the traditional relational data tables
Let's see what's changed with the traditional table operation .
Inquire
Mysql-js> db. City.select (["Name", "CountryCode"])
. WHERE ("Name like ' Z% '")
["Name" in Select, "CountryCode"] means that only the information of these two columns is returned, where is the query condition
Parameter binding can be performed on a query condition
Mysql-js> db. City.select (["Name", "CountryCode"])
. WHERE ("Name like:name")
. Bind ("name", "Z%")
Sort and limit the number of results
Mysql-js> db. Country.select (["Code", "Name"])
. by (["Name desc"]). Limit (3)
Insert
Mysql-js> db. City.insert ("ID", "Name", "CountryCode")
. VALUES (1, "Little Falls", "USA")
. VALUES (2, "Happy Valley", "USA")
Multiple value can be defined to insert more than one record
Modify
Mysql-js> db. City.update ()
. Set ("Name", "Beijing")
. WHERE ("Name = ' Peking '")
Delete
Mysql-js> db. City.delete ()
. WHERE ("Name = ' Olympia '")
As you can see, it's easy to understand how a function chain is used, and it's friendly for developers who are unfamiliar with SQL.
Action Collection
Add a document
Executes the Add method of the Countryinfo collection, and the parameter is the JSON data for the new document
Inquire
Using the Find method to perform a query operation, the following example does not display the resulting information because the query results are too long
(1) List all documents
Mysql-js> db. Countryinfo.find ()
(2) Conditional query
Mysql-js> db. Countryinfo.find ("_id = ' 888 '")
Mysql-js> db. Countryinfo.find ("GNP > Demographics . Population < 100 ")
(3) Parameter binding
Mysql-js> db. Countryinfo.find ("Name =: Country")
. Bind ("Country", "Italy")
(4) Limit and sort
Mysql-js> db. Countryinfo.find ()
. Sort (["Indepyear desc"])
. Limit (8). Skip (1)
Modifying a document
Using the collection's modify and set method chain to modify the document, the Modify parameter indicates the search criteria for the target document, and the set parameter gives the modification scheme
Can cooperate with modify not only set, but also unset, Arrayappend, Arrayinsert, Arraydelete and other methods
Delete a document
(1) Delete eligible documents
Mysql-js> db. Countryinfo.remove ("_id = ' SEA '")
(2) Sort the result set first, then delete the first one
Mysql-js> db. Countryinfo.remove ()
. Sort (["Name desc"]). Limit (1)
This new approach reduces the difficulty of development, such as a developer familiar with MongoDB, but not familiar with SQL, it is now easy to use MySQL
Mysql Shell Console---mysqlsh