Beta Draft: 2016-08-16
Table of Contents
This section explains how to build expressions using X DevAPI.
When working with MySQL expressions used in CRUD, statements can be specified in two ways. The first is to use strings to formulate the expressions which should be familiar if you have developed code with SQL before. The other method is to use Expression Builder functionality.
Defining string expressions is straight forward as these are easy to read and write. The disadvantage is that they need to be parsed before they can be transfered to the MySQL Server. In addition, type checking can only be done at runtime.
MySQL Shell JavaScript Code
// Using a string expression to get all documents that
// have the name field starting with 'S'
var myDocs = myColl.find('name like :name').bind('name', 'S%').execute();
All implementations can use the syntax illustrated above.
Boolean expression strings can be used when filtering
collections or tables using operations, such as
find() and remove(). The
expression is evaluated once for each document or row.
The following example of a boolean expression string uses
find() to search for all documents with a
“red” color attribute from the collection
“apples”:
apples.find('color = "red"').execute()
Similarly, to delete all red apples:
apples.remove('color = "red"').execute()
Value expression strings are used to compute a value which can
then be assigned to a given field or column. This is necessary
for both modify() and
update(), as well as computing values in
documents at insertion time.
An example use of a value expression string would be to
increment a counter. The expr() function is
used to wrap strings where they would otherwise be interpreted
literally. For example, to increment a counter:
// the expression is evaluated on the server
collection.modify().set("counter", expr("counter + 1")).execute()
If you do not wrap the string with expr(), it
would be assigning the literal string "counter + 1" to the
"counter" member:
// equivalent to directly assigning a string: counter = "counter + 1"
collection.modify().set("counter", "counter + 1").execute()