使用空数组的cosmos db查询自我连接 [英] cosmos db query self join with null array

查看:62
本文介绍了使用空数组的cosmos db查询自我连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从cosmos db azure获取记录.有以下两个条件-

I want to get records from cosmos db azure. there are two conditions as below-

SELECT p.name, p.id
FROM Product p join grp in p.groups
where grp.name = 'abc' or ARRAY_LENGTH(grp) = 0 

第一部分是给我没有结果或条件的结果,即(文档1).但是我也想要那些在p.groups中不包含任何记录的记录,即(文档2).

The first part is giving me result without or condition i.e.(document 1). But i want those records also which does not contain any record in p.groups i.e.(document 2).

我的数据:

文档1-

"id": "123",
"name": "Test Name",
"groups": [
        {
            "name": "abc"        }
]

文档2-

"id": "1234",
"name": "Test Name Blank",
"groups": []

预期输出:

[
    {
        "name": "Test Name",
        "id": "123"
    },
{
        "name": "Test Name Blank",
        "id": "1234"
    },
]

推荐答案

您可以使用以下sql:

You could use following sql :

SELECT c.id,c.name from c where ARRAY_LENGTH(c.groups) =0 or ARRAY_CONTAINS(c.groups, {"name": "abc"}, true)

输出结果:

希望它对您有帮助.

这篇关于使用空数组的cosmos db查询自我连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆