7.1作为关系表的集合

寻求使用Documents存储标准SQL列的应用程序可以将集合强制转换为表。方法将集合作为Table对象获取Schema.getCollectionAsTable ()函数。从那一刻起,它就像普通的桌子一样被对待。在SQL CRUD操作中,可以使用以下语法访问文档值:

doc - >“.field美元”

doc - >“.field美元”用于访问文档顶级字段。还可以指定更复杂的路径。

医生——>“.some.field.like美元[3]。”

类将集合作为表提取之后Schema.getCollectionAsTable ()函数,所有的SQL CRUD操作都可以使用。使用文档访问的语法,您可以从集合的Documents和额外的SQL列中选择数据。

对象中插入JSON文档字符串,示例如下医生字段。

MySQL Shell JavaScript代码

//获取客户集合为表var customers = db.getCollectionAsTable('customers');customers.insert(“文档”)。值(" {" _id ": " 001 ", "名称":“安娜”,“last_name”:“席尔瓦”}”). execute ();//现在执行查找操作来检索插入的文档var result = customers.select(["doc->'$.name'", "doc->'$.last_name'"]).where("doc->'$. name'").where("doc->'$. last_name'")_id' = '001'").execute();var record = result.fetchOne();print ("Name: " + record[0]);print ("Last Name: " + record[1]);

MySQL Shell Python代码

#获取客户集合为表customers = db.get_collection_as_table('customers') customers.insert('doc')。values('{"_id":"001", "name": "Ana", "last_name": "Silva"}').execute() #现在执行查找操作来检索插入的文档result = customers.select(["doc->'$.name'", "doc->'$.last_name'"]).where("doc->'$. last_name'")。.execute() record = result.fetch_one() print("Name: %s\n" % record[0]) print("Last Name: %s\n" % record[1])

js JavaScript代码

//获取客户集合为表var customers = db.getCollectionAsTable('customers');customers.insert(“文档”)。values('{"_id":"001", "name": "Ana"}').execute();

c#代码

//获取客户集合为表var customers = db.GetCollectionAsTable("customers");customers.Insert(“文档”)。值(“{\“_id \”:1,\“\”,\“安娜\”}"). execute ();

Python代码

#获取客户集合为表customers = db.get_collection_as_table("customers") customers.insert('doc')。values({'_id':'001', 'name': 'Ana', 'last_name': 'Silva'}).execute() #现在执行查找操作来检索插入的文档result = customers.select(["doc->'$.name'", "doc->'$.last_name'"]).where("doc->'$. last_name'")。_id' = '001'").execute() record = result.fetch_one() print('Name: {0}'.format(record[0])) print('Last Name: {0}'.format(record[1]))

Java代码

//获取客户集合为表table customers = db.getCollectionAsTable("customers");customers.insert(“文档”)。值(“{\“\”:\“安娜\“}”). execute ();

c++代码

//获取客户集合为表table customers = db.getCollectionAsTable("customers");customers.insert(“医生”). values (R”({" _id ": " 001 ",“名字”:“安娜”,“last_name”:“席尔瓦”})”). execute ();//现在执行查找操作来检索插入的文档RowResult result = customers.select("doc->'$.name'", "doc->'$.last_name'") .where("doc->'$. name'")_id' = '001'").execute();行记录= result.fetchOne();cout << "Name: " << record[0] << endl;cout << "Last Name: " << record[1] << endl;