9.6 JSONB陣列I具有下表:查詢Postgres的對象
CREATE TABLE trip
(
id SERIAL PRIMARY KEY ,
gps_data_json jsonb NOT NULL
);
的JSON在gps_data_json包含的具有以下字段跳閘對象的數組(下面樣本數據):
- 模式
- 時間戳
- 緯度
- 經度
我試圖讓所有包含特定「模式」的行。
SELECT * FROM trip
where gps_data_json ->> 'mode' = 'WALK';
我很確定我使用的是 - >>操作錯了,但我不能確定誰告訴查詢該JSONB場是對象的數組?
的樣本數據:
INSERT INTO trip (gps_data_json) VALUES
('[
{
"latitude": 47.063480377197266,
"timestamp": 1503056880725,
"mode": "TRAIN",
"longitude": 15.450349807739258
},
{
"latitude": 47.06362533569336,
"timestamp": 1503056882725,
"mode": "WALK",
"longitude": 15.450264930725098
}
]');
INSERT INTO trip (gps_data_json) VALUES
('[
{
"latitude": 47.063480377197266,
"timestamp": 1503056880725,
"mode": "BUS",
"longitude": 15.450349807739258
},
{
"latitude": 47.06362533569336,
"timestamp": 1503056882725,
"mode": "WALK",
"longitude": 15.450264930725098
}
]');
您可以使用[jsonb_array_elements](https://www.postgresql.org/docs/current/static/functions- json.html)並避免強制轉換。 –
是的,我只是錯過了它,糾正了,謝謝 – jlandercy