SQL查询以获取尚未处理的通知

时间:2019-12-18 14:25:58

标签: sql azure-cosmosdb azure-cosmosdb-sqlapi

我有一个CosmosDb容器,用于存储通过SignalR广播的通知。我已经存储了这2条通知:

[
    {
        "createdOn": "2019-12-18T09:48:21.4556896Z",
        "disposed": [
            {
                "disposedBy": "anotherAdmin@mail.com",
                "disposedOn": "2019/11/16T12:00:00"
            }
        ],
        "id": "615b9335-f78f-4a53-9e33-74e1b601d8bd",
        "message": [
            {
                "culture": "en-US",
                "key": "Title",
                "value": "Controller notification!"
            },
            {
                "culture": "en-US",
                "key": "Message",
                "value": "This is an info message received from PushNotifications controller!"
            }
        ],
        "target": "administrators",
        "ttl": 86400,
        "_rid": "iNZhANsdVb0XAQAAAAAAAA==",
        "_self": "dbs/iNZhAA==/colls/iNZhANsdVb0=/docs/iNZhANsdVb0XAQAAAAAAAA==/",
        "_etag": "\"0500558f-0000-0d00-0000-5dfa08d00000\"",
        "_attachments": "attachments/",
        "_ts": 1576667344
    },
    {
        "createdOn": "2019-12-18T09:48:30.4778759Z",
        "disposed": [
            {
                "disposedBy": "admin@mail.com",
                "disposedOn": "2019/11/16T12:00:00"
            },
            {
                "disposedBy": "anotherAdmin@mail.com",
                "disposedOn": "2019/11/16T12:05:03"
            }
        ],
        "id": "287d1ce0-017e-40b5-ab47-9e2222448b33",
        "message": [
            {
                "culture": "en-US",
                "key": "Title",
                "value": "Controller notification!"
            },
            {
                "culture": "en-US",
                "key": "Message",
                "value": "This is an info message received from PushNotifications controller!"
            }
        ],
        "target": "administrators",
        "ttl": 86400,
        "_rid": "iNZhANsdVb0YAQAAAAAAAA==",
        "_self": "dbs/iNZhAA==/colls/iNZhANsdVb0=/docs/iNZhANsdVb0YAQAAAAAAAA==/",
        "_etag": "\"050092b6-0000-0d00-0000-5dfa0c750000\"",
        "_attachments": "attachments/",
        "_ts": 1576668277
    }
]

现在,我想创建一个查询,以获取"target"administrators并且被admin@mail.com处置的不是的所有通知。

我尝试了一些查询,例如:

SELECT value notifications 
FROM notifications JOIN
(SELECT value disposed.disposedBy 
    FROM disposed in notifications.disposed 
    WHERE disposed.disposedBy != "admin@mail.com") 
WHERE notifications.target = "administrators"

SELECT value notifications 
FROM notifications JOIN 
(SELECT value disposed.disposedBy 
    FROM disposed IN notifications.disposed 
    WHERE disposed.disposedBy != "admin@mail.com") 
    JOIN (SELECT value notifications 
    WHERE notifications.target = "administrators")

甚至

SELECT value notifications
FROM notifications JOIN 
(SELECT value disposed.disposedBy 
    FROM disposed IN notifications.disposed
    WHERE ARRAY_CONTAINS(disposed.disposedBy, "admin@mail.com")) 

但是他们都没有给我仅收到一个通知的预期结果(编号:615b9335-f78f-4a53-9e33-74e1b601d8bd)。

如何构建此查询以获取针对某些特定目标且未由某些用户处置的通知?

编辑1:我已经弄清楚了如何使用查询

将处置方式更改为disposed: ["user@mail.com", "anotherUser@mail.com"]这样的字符串数组的方法
SELECT value notifications 
FROM notifications 
WHERE NOT Array_Contains(notifications.disposed, "user@mail.com")
AND notifications.target = "administrators"

1 个答案:

答案 0 :(得分:1)

您几乎明白了。请尝试我的SQL:

SELECT value notifications 
FROM notifications
WHERE notifications.target = "administrators"
and not Array_contains(notifications.disposed,{ "disposedBy": "admin@mail.com"},true)

输出:

enter image description here