Beta Draft: 2016-08-16
Table of Contents
After seeing how to work with documents and how to work with relational tables, this section explains how to combine the two and work with both at the same time.
It can be beneficial to use documents for very specific tasks inside an application and rely on relational tables for other tasks. Or a very simple document only application can outgrow the document model and incrementally integrate or move to a more powerful relational database. This way the advantages of both documents and relational tables can be combined. SQL tables contribute strictly typed value semantics, predictable and optimized storage. Documents contribute type flexibility, schema flexibility and non-scalar types.
Applications that seek to store standard SQL columns with
Documents can cast a collection to a table. In this case a
collection can be fetched as a Table object with the
Schema.getCollectionAsTable() function. From
that moment on it is treated as a regular table. Document values
can be accessed in SQL CRUD operations using the following syntax:
doc->'$.field'
doc->'$.field' is used to access the document
top level fields. More complex paths can be specified as well.
doc->'$.some.field.like[3].this'
Once a collection has been fetched as a table with the
Schema.getCollectionAsTable() function, all SQL
CRUD operations can be used. Using the syntax for document access,
you can select data from the Documents of the Collection and the
extra SQL columns.
The following examples show how to insert a JSON document string
into the doc field.
MySQL Shell JavaScript Code
// Get the customers collection as a table
var customers = db.getCollectionAsTable('customers');
customers.insert('doc').values('{"_id":"001", "name": "mike", "last_name": "connor"}').execute();
// Now do a find operation to retrieve the inserted document
var result = customers.select(["doc->'$.name'", "doc->'$.last_name'"]).where("doc->'$._id' = '001'").execute();
var record = result.fetchOne();
print ("Name : " + record[0]);
print ("Last Name : " + record[1]);
MySQL Shell Python Code
# Get the customers collection as a table
customers = db.getCollectionAsTable('customers')
customers.insert('doc').values('{"_id":"001", "name": "mike", "last_name": "connor"}').execute()
# Now do a find operation to retrieve the inserted document
result = customers.select(["doc->'$.name'", "doc->'$.last_name'"]).where("doc->'$._id' = '001'").execute()
record = result.fetchOne()
print "Name : %s\n" % record[0]
print "Last Name : %s\n" % record[1]
Node.js JavaScript Code
// Get the customers collection as a table
var customers = db.getCollectionAsTable('customers');
customers.insert('doc').values('{"_id":"001", "name": "mike"}').execute();
C# Code
{
// Get the customers collection as a table
var customers = db.GetCollectionAsTable("customers");
customers.Insert("doc").Values("{ \"_id\": 1, \"name\": \"mike\" }").Execute();
}
Java Code
// Get the customers collection as a table
Table customers = db.getCollectionAsTable("customers");
customers.insert("doc").values("{\\"name\\": \\"mike\\"}").execute();
C++ Code
// getCollectionAsTable() is not yet implemented in Connector/C++.