JSONコレクション表の例

この項では、ショッピング・アプリケーション用に作成されたサンプルJSONコレクション表について説明します。

表のDDL:
CREATE TABLE IF NOT EXISTS storeAcct (contactPhone STRING, PRIMARY KEY(SHARD(contactPhone)))
      AS JSON COLLECTION 

この表は、買物客のcontactPhoneが主キーであるドキュメントのコレクションです。行は、個々の買物客のレコードを表します。個々の行にドキュメントの同じフィールドを含める必要はありません。買物客のプリファレンス(nameaddressemailnotifyなど)は、ドキュメントの最上位フィールドとして格納されます。ドキュメントには、ショッピング関連情報を含む任意の数のJSONフィールド(wishlistcartordersなど)を含めることができます。

JSON配列wishlistには、買物客が欲しい物リストに登録したアイテムが含まれています。この配列の各要素には、欲しい物リストのアイテムの製品名と価格詳細が格納されるitempriceperunitなどのネストされたJSONフィールドが含まれています。

JSON配列cartには、買物客が購入するつもりの製品が含まれています。この配列の各要素には、製品名、数量および各製品の価格を格納するネストされたJSONフィールド(itemquantitypriceperunitなど)が含まれています。

JSON配列ordersには、買物客が購入した製品が含まれています。この配列の各要素には、オーダー番号、製品名、単価、製品の配送予定日およびオーダーのステータスが格納されるorderIDitempriceperunitEstDeliverystatusなどのネストされたJSONフィールドが含まれています。

次のコードは、ショッピング・アプリケーションの表にデータを挿入します。このデータを使用すると、トピックの例の説明をたどっていくことができます。
insert into  storeAcct(contactPhone, firstName, lastName, address, cart) values("1817113382", "Adam", "Smith", {"street" : "Tex Ave", "number" : 401, "city" : "Houston", "state" : "TX",   "zip" : 95085}, [{"item" : "handbag", "quantity" : 1, "priceperunit" : 350},{"item" : "Lego", "quantity" : 1, "priceperunit" : 5500}]) RETURNING *;

insert into  storeAcct(contactPhone, firstName, lastName, gender, address, notify, cart, wishlist) values("1917113999", "Sharon", "Willard", "F", {"street" : "Maine", "number" : 501, "city" : "San Jose", "state" : "San Francisco",   "zip" : 95095},"yes", [{"item" : "wallet", "quantity" : 2, "priceperunit" : 950},{"item" : "wall art", "quantity" : 1, "priceperunit" : 9500}], [{"item" : "Tshirt", "priceperunit" : 500},{"item" : "Jenga", "priceperunit" : 850}]) RETURNING *;

insert into  storeAcct(contactPhone, firstName, lastName, address, notify, cart, orders) values("1617114988", "Lorenzo", "Phil", {"Dropbox" : "Presidency College", "city" : "Kansas City", "state" : "Alabama",   "zip" : 95065},"yes", [{"item" : "A4 sheets", "quantity" : 2, "priceperunit" : 500},{"item" : "Mobile Holder", "quantity" : 1, "priceperunit" : 700}], [{"orderID" : "101200", "item" : "AG Novels 1", "EstDelivery" : "2023-11-15", "priceperunit" : 950, "status" : "Preparing to dispatch"},{"orderID" : "101200", "item" : "Wallpaper", "EstDelivery" : "2023-11-01", "priceperunit" : 950, "status" : "Transit"}]) RETURNING *;

insert into  storeAcct(contactPhone, firstName, lastName, address, cart, orders) values("1517113582", "Dierdre", "Amador", {"street" : "Tex Ave", "number" : 651, "city" : "Houston", "state" : "TX",   "zip" : 95085}, NULL, [{"orderID" : "201200", "item" : "handbag", "EstDelivery" : "2023-11-01", "priceperunit" : 350},{"orderID" : "201201", "item" : "Lego", "EstDelivery" : "2023-11-01", "priceperunit" : 5500}]) RETURNING *;

insert into  storeAcct(contactPhone, firstName, lastName, address, notify, cart, orders) values("1417114488", "Doris", "Martin", {"Dropbox" : "Presidency College", "city" : "Kansas City", "state" : "Alabama",   "zip" : 95065},"yes", [{"item" : "Notebooks", "quantity" : 2, "priceperunit" : 50},{"item" : "Pens", "quantity" : 2, "priceperunit" : 50}], [{"orderID" : "301200", "item" : "Laptop Bag", "EstDelivery" : "2023-11-15", "priceperunit" : 1950, "status" : "Preparing to dispatch"},{"orderID" : "301200", "item" : "Mouse", "EstDelivery" : "2023-11-02", "priceperunit" : 950, "status" : "Transit"}]) RETURNING *;