基于键值对过滤存储在Postgres中的JSON数据



我有一个JSON对象存储为JSONBPostgres列下的数据库cart.这是2个用户的购物车数据

"items": [
{
"name": "T-shirt",
"price": 250,
"item_id": 111,
"quantity": 1
},
{
"name": "Trousers",
"price": 600,
"item_id": 222,
"quantity": 1
}
]
}
&&&&
{
"items": [
{
"name": "Jeans",
"price": 250,
"item_id": 333,
"quantity": 1
},
{
"name": "Trousers",
"price": 600,
"item_id": 444,
"quantity": 1
}
]
}

此数据存储在列名cart下. 我尝试使用Gin索引,但不清楚我在做什么。我应该如何能够查询数据,这样我就可以找到所有用户的列表,有裤子作为一个项目在他们的购物车Postgres?此外,我是这个实现的新手,热衷于学习,所以如果通过Golang实现会很有帮助.
谢谢,Pushkar Singh

USING gin ((cart->'items') jsonb_path_ops);
SELECT * FROM order2 WHERE cart->'items' @> '[{"name":"Trousers"}]';

具有:

查询JSON类型

中的数组元素在JSON数组中查找元素的索引

最新更新