Spec-Zone.ru › MySQL Connectors 1.0

7.1 Коллекции как реляционные таблицы

Приложения, которые стремятся хранить стандартные столбцы SQL вместе с документами, могут преобразовать коллекцию в таблицу. В этом случае коллекцию можно извлечь как объект Table с помощью функции 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;

© 2025 Oracle
Licensed under the GPLv2 License.
https://docs.oracle.com/cd/E17952_01/x-devapi-userguide-en/collections-as-relational-tables.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API