文件首頁
X DevAPI 使用者指南
下載本手冊
PDF (美式信紙) - 1.4Mb
PDF (A4) - 1.4Mb


X DevAPI 使用者指南  /  使用關聯式表格和文件  /  將集合視為關聯式表格

7.1 將集合視為關聯式表格

想要使用文件儲存標準 SQL 資料行的應用程式,可以將集合轉換為表格。在此情況下,可以使用 Schema.getCollectionAsTable() 函式將集合擷取為表格物件。從那時起,它就會被視為一般的表格。可以使用下列語法在 SQL CRUD 操作中存取文件值

doc->'$.field'

doc->'$.field' 用於存取文件最上層的欄位。也可以指定更複雜的路徑。

doc->'$.some.field.like[3].this'

一旦使用 Schema.getCollectionAsTable() 函式將集合擷取為表格後,就可以使用所有 SQL CRUD 操作。使用存取文件的語法,您可以從集合的文件和額外的 SQL 資料行中選取資料。

下列範例顯示如何將 JSON 文件字串插入 doc 欄位。

MySQL Shell JavaScript 程式碼

// Get the customers collection as a table
var customers = db.getCollectionAsTable('customers');
customers.insert('doc').values('{"_id":"001", "name": "Ana", "last_name": "Silva"}').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 程式碼

# Get the customers collection as a table
customers = db.get_collection_as_table('customers')
customers.insert('doc').values('{"_id":"001", "name": "Ana", "last_name": "Silva"}').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.fetch_one()

print("Name : %s\n"  % record[0])
print("Last Name : %s\n"  % record[1])

Node.js JavaScript 程式碼

// Get the customers collection as a table
var customers = db.getCollectionAsTable('customers');
customers.insert('doc').values('{"_id":"001", "name": "Ana"}').execute();

C# 程式碼

// Get the customers collection as a table
var customers = db.GetCollectionAsTable("customers");
customers.Insert("doc").Values("{ \"_id\": 1, \"name\": \"Ana\" }").Execute();

Python 程式碼

# Get the customers collection as a table
customers = db.get_collection_as_table("customers")
customers.insert('doc').values({'_id':'001', 'name': 'Ana', 'last_name': 'Silva'}).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.fetch_one()

print('Name : {0}'.format(record[0]))
print('Last Name : {0}'.format(record[1]))

Java 程式碼

// Get the customers collection as a table
Table customers = db.getCollectionAsTable("customers");
customers.insert("doc").values("{\"name\": \"Ana\"}").execute();

C++ 程式碼

// Get the customers collection as a table
Table customers = db.getCollectionAsTable("customers");
customers.insert("doc")
         .values(R"({"_id":"001", "name": "Ana", "last_name": "Silva"})").execute();

// Now do a find operation to retrieve the inserted document
RowResult result = customers.select("doc->'$.name'", "doc->'$.last_name'")
                            .where("doc->'$._id' = '001'").execute();

Row record = result.fetchOne();
cout << "Name : " << record[0] << endl;
cout << "Last Name : " << record[1] << endl;