我在CockroachDB v2.0测试版中有一个简单的表:
CREATE TABLE account (
id UUID NOT NULL DEFAULT uuid_v4()::UUID,
acct JSON NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
INVERTED INDEX account_acct_idx (acct),
FAMILY "primary" (id, acct)
) 我可以运行select查询来查找acct>properties下的特定属性,如下所示:
select acct->>'id' from account where acct @> '{"properties": {"foo": "bar"}}';有没有办法选择Json blob的子集,比如嵌套属性?下面这样的代码会很有帮助:
select acct->>'id', acct->>'properties:description' from account
where acct @> '{"properties": {"foo": "bar"}}';提前感谢您的任何提示!
干杯,~
发布于 2020-12-16 05:03:34
正如Jordan在上面的评论中提到的,您已经可以使用问题中引用的运算符获得嵌套的JSON对象。使用您的示例,我可以访问嵌套对象,如下所示:
> CREATE TABLE account (
id UUID NOT NULL DEFAULT uuid_v4()::UUID,
acct JSON NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
INVERTED INDEX account_acct_idx (acct),
FAMILY "primary" (id, acct)
);
CREATE TABLE
> INSERT INTO account (acct) VALUES ('{"properties": {"foo": "bar"}}');
INSERT 1
> SELECT * FROM account;
id | acct
---------------------------------------+---------------------------------
6fe08368-7720-4ddd-885e-75437b4e0267 | {"properties": {"foo": "bar"}}
(1 row)
> SELECT acct->>'properties' FROM account WHERE acct @> '{"properties": {"foo": "bar"}}';
?column?
------------------
{"foo": "bar"}
(1 row)请注意,->>返回嵌套JSON对象的字符串表示。正如我们的JSON docs中所描述的,操作符->可用于返回对象本身。如果您想要访问更深的嵌套对象,这还允许您链接操作符。例如:
> select acct->'properties'->'foo' from account;
?column?
------------
"bar"
(1 row)https://stackoverflow.com/questions/49582487
复制相似问题