[关闭]
@WrRan 2016-11-17T09:31:19.000000Z 字数 1822 阅读 1273

node-oracledb

12. Oracle Database 12.1 JSON Datatype

Oracle Database 12.1.0.2 introduced native support for JSON data. You
can use JSON with relational database features, including
transactions, indexing, declarative querying, and views. You can
project JSON data relationally, making it available for relational
processes and tools.

JSON data in the database is stored as BLOB, CLOB or VARCHAR2 data.
This means that node-oracledb can easily insert and query it.

As an example, the following table has a PO_DOCUMENT column that is
enforced to be JSON:

  1. CREATE TABLE po (po_document VARCHAR2(4000) CHECK (po_document IS JSON));

To insert data using node-oracledb:

  1. var data = { customerId: 100, item: 1234, quantity: 2 };
  2. var s = JSON.stringify(data); // change JavaScript value to a JSON string
  3. connection.execute(
  4. "INSERT INTO po (po_document) VALUES (:bv)",
  5. [s] // bind the JSON string
  6. function (err) {
  7. . . .
  8. });

Queries can access JSON with Oracle JSON path expressions. These
expressions are matched by Oracle SQL functions and conditions to
select portions of the JSON data. Path expressions can use wildcards
and array ranges. An example is $.friends which is the value of
JSON field friends.

Oracle provides SQL functions and conditions to create, query, and
operate on JSON data stored in the database. An example is the Oracle
SQL Function JSON_TABLE which projects JSON data to a relational
format effectively making it usable like an inline relational view.
Another example is JSON_EXISTS which tests for the existence of a
particular value within some JSON data:

This example looks for JSON entries that have a quantity field:

  1. conn.execute(
  2. "SELECT po_document FROM po WHERE JSON_EXISTS (po_document, '$.quantity')",
  3. function(err, result)
  4. {
  5. if (err) {
  6. . . .
  7. } else {
  8. var js = JSON.parse(result.rows[0][0]); // show only first record in this example
  9. console.log('Query results: ', js);
  10. }
  11. });

After the previous INSERT example, this query would display:

  1. { customerId: 100, item: 1234, quantity: 2 }

See selectjson.js
and selectjsonclob.js
for runnable examples.

For more information about using JSON in Oracle Database see JSON in
Oracle Database
.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注