在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.

我将假设您拥有PostgreSQLpgAdmin之类的东西。

让我们开始吧! (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_purchased) V
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值