Master MySQL Shell’s JavaScript Mode: Query, Insert, Update Without SQL
This guide demonstrates how MySQL 5.7.12’s new JavaScript‑based shell lets you query, insert, update, and delete both traditional tables and JSON document collections using intuitive method‑chaining syntax, eliminating the need for SQL knowledge and simplifying migration from MongoDB‑style workflows.
Before MySQL 5.7.12 developers typically used the classic SQL console, e.g., mysql> select * from tablename;.
Starting with version 5.7.12 MySQL introduced JSON document support and a brand‑new JavaScript‑based shell that allows database operations without writing SQL, using a natural code style such as:
db.news.insert("create_date", "title") .values("2016-04-10", "hello")You can run these commands directly in the MySQL Shell or via the new driver libraries.
The JavaScript version of the shell is started with:
mysqlsh -u root world_xWhen the prompt shows mysql-js>, the interactive language is JavaScript. Because MySQL now handles JSON documents, you can work with both traditional relational tables and document collections.
Traditional table operations
Query a table:
db.City.select(["Name", "CountryCode"]).where("Name like 'Z%'")Parameter binding:
db.City.select(["Name", "CountryCode"]).where("Name like :name").bind("name", "Z%")Order and limit results:
db.Country.select(["Code", "Name"]).orderBy(["Name desc"]).limit(3)Insert rows:
db.City.insert("ID", "Name", "CountryCode").values(1, "Little Falls", "USA").values(2, "Happy Valley", "USA")Update rows:
db.City.update().set("Name", "Beijing").where("Name = 'Peking'")Delete rows: db.City.delete().where("Name = 'Olympia'") Document‑collection operations
Add a document to a collection (e.g., CountryInfo):
Find documents: db.CountryInfo.find() – list all documents. db.CountryInfo.find("_id = '888'") – conditional query.
db.CountryInfo.find("GNP > 50 and demographics.Population < 100")Parameter binding in queries:
db.CountryInfo.find("Name = :country").bind("country", "Italy")Sorting, limiting and skipping:
db.CountryInfo.find().sort(["IndepYear desc"]).limit(8).skip(1)Modify a document:
db.CountryInfo.modify("_id = '123'").set("field", "value")– you can also use unset, arrayAppend, arrayInsert, arrayDelete, etc.
Remove documents: db.CountryInfo.remove("_id = 'SEA'") – delete by condition. db.CountryInfo.remove().sort(["Name desc"]).limit(1) – sort then delete the first match.
This new method‑chaining approach makes MySQL operations intuitive, especially for developers familiar with MongoDB who may not know SQL.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
