将新的 JSON 文档插入 Oracle Autonomous JSON 数据库

Inserting new JSON document into Oracle Autonomous JSON Database

使用数据库操作(SQL Developer Web),单击 'New JSON Document' 按钮将新的 JSON 文档添加到集合中非常容易。

集合当然在Oracle中实际上是一个table,而table本身有多个列:

我已经在 ORDS 中使用 PL/SQL 处理程序创建了模块。虽然我可以在这里使用

更新 JSON 文档

UPDATE "Collection" SET json_document = '{"key": "value"}' WHERE JSON_VALUE(json_document, '$.id') = :id'

我无法使用

轻松添加新文档

INSERT INTO "Collection" (json_document) VALUES ('{"key": "value"}')

因为id设置为PK列,必须指定。我如何使用 PL/SQL 在其他地方添加带有自动生成字段的新文档?或者我应该使用 SODA for PL/SQL 来达到这个目的吗?

谢谢!

您使用 dbms_soda 包访问该集合。然后使用soda_colletion_t上的方法对其进行操作。

例如:

soda create students;
declare
  collection  soda_collection_t;
  document    soda_document_t;
  status      number;
begin
  -- open the collection
  collection := dbms_soda.open_collection('students');
  document   := soda_document_t(
    b_content => utl_raw.cast_to_raw ( 
      '{"id":1,"name":"John Blaha","class":"1980","courses":["c1","c2"]}'
    )
  );

  -- insert a document
  status := collection.insert_one(document);
end;
/

select * from students;

ID                               CREATED_ON               LAST_MODIFIED            VERSION                          JSON_DOCUMENT   
-------------------------------- ------------------------ ------------------------ -------------------------------- --------------- 
A92F68753B384F87BF12557AC38098CB 2021-12-22T14:15:12.831Z 2021-12-22T14:15:12.831Z FE8C80FED46A4F18BFA070EF46073F43 [object Object] 

有关如何将 SODA 用于 PL/SQL 的完整文档和示例,请参阅 here