在PostgreSQL中使用JSONB对象数组

数据库提示 (Database Tips)

In my last article, we focused on turning a PostgreSQL jsonb column, that contained an Array of Objects, into a recordset, so that it could be queried in a relational manner.

在我的上一篇文章中,我们专注于将包含对象数组的PostgreSQL jsonb列转换为记录集,以便可以以关系方式对其进行查询。

我们将要做什么 (What We Will Do)

In this article, I’d like to continue our work with a jsonb column containing an Array of Objects and explore how to,

在本文中,我想继续研究包含对象数组的jsonb列,并探讨如何实现这一点,

  • Get all Objects in the Array.

    获取数组中的所有对象。
  • Get a specific Object from the Array by position in the Array. When the position is known and when we have to discover it.

    通过在数组中的位置从数组中获取特定的对象。 何时知道位置以及何时必须找到它。
  • Add a json Object to the Array.

    向数组添加一个json对象。
  • Remove a specific Object from the Array.

    从阵列中删除特定的对象。

We will not only look at the query techniques but think about these as being API end-points and what they might mean for the front-end in, say, a PERN stack.

我们不仅会研究查询技术,还将这些技术视为API端点,以及它们对PERN堆栈中的前端可能意味着什么。

Sample Data

样本数据

Our sample data in a jsonb column will look similar to this,

我们在jsonb列中的示例数据看起来与此类似,

[{
       
"productid": 3,
"name": "Virtual Keyboard",
"price": 150}, {
"productid": 1,
"name": "Dell 123 Laptop Computer",
"price": 1300},
{
"productid": 8,
"name": "LG Ultrawide Monitor",
"price": 190}
]

This might represent products purchased by a customer. We want to Add new purchases, Get all items purchased, Get a specific item purchased and Remove an item purchased.

这可能代表客户购买的产品。 我们要添加新购买,购买所有物品,购买特定物品并删除购买的物品。

In the last article we worked with the jsonb_to_recordset() function.

在上一篇文章中,我们使用了jsonb_to_recordset()函数。

In this article, we will make extensive use of the jsonb_array_elements() function.

在本文中,我们将广泛使用jsonb_array_elements()函数。

假设条件 (Assumptions)

I will assume you have PostgreSQL and something along the lines of pgAdmin.

我将假设您拥有PostgreSQL和pgAdmin之类的东西。

让我们开始吧! (Let’s Get Started!)

建立我们的数据 (Building Our Data)

  1. Open pgAdmin and create a database as desired.

    打开pgAdmin并根据需要创建数据库。

  2. Right-Click on the database name and choose Query Tool.

    右键单击数据库名称,然后选择“ 查询工具”

  3. Run the snippet below to create a simple table that will have an id, purchaser name and a jsonb column that stores an array of json objects, which will store items purchased.

    运行下面的代码片段以创建一个简单的表,该表将具有一个id,购买者名称和一个jsonb列,该列存储json对象数组,该数组将存储购买的商品。

CREATE TABLE public.purchases
(
id serial PRIMARY KEY,
purchaser varchar(50),
items_purchased jsonb
);

4. Run the snippet below to insert four records in to the table.

4.运行下面的代码片段,将四个记录插入到表中。

INSERT INTO purchases (purchaser,items_purchased) VALUES ('Bob',
'[{
"productid": 1,
"name": "Dell 123 Laptop Computer",
"price": 1300},
{
"productid": 2,
"name": "Mechanical Keyboard",
"price": 120}
]');INSERT INTO purchases (purchaser,items_purchased) VALUES('Carol',
'[{
"productid": 3,
"name": "Virtual Keyboard",
"price": 150}, {
"productid": 1,
"name": "Dell 123 Laptop Computer",
"price": 1300},
{
"productid": 8,
"name": "LG Ultrawide Monitor",
"price": 190}
]');INSERT INTO purchases (purchaser,items_purchased) VALUES ('Ted',
'[{
"productid": 6,
"name": "Ergonomic Keyboard",
"price": 90},
{
"productid": 7,
"name": "Dell 789 Desktop Computer",
"price": 120}
]');INSERT INTO purchases (purchaser,items_purc

你可能感兴趣的:(java,python)