【问题标题】:Cosmos DB - Select root document based on child dataCosmos DB - 根据子数据选择根文档
【发布时间】:2019-07-25 20:17:48
【问题描述】:

对不起,如果这是一个新手问题,但我是 Cosmos DB 的新手。

我正在尝试从我的集合中选择所有子元素匹配指定(多个)条件的根文档。

假设您有一个 ORDER 文档,其中包含 ORDERITEMS 作为子数据文档,我需要做的是查询已订购特定产品的所有订单,并返回整个订单文档。

[
    {
        "order": {
            "id": "1",
            "orderiems": [
                {
                    "partcode": "A",
                    "qty": "4"
                },
                {
                    "partcode": "B",
                    "qty": "4"
                },
                {
                    "partcode": "C",
                    "qty": "4"
                }
            ]
        }
    },
    {
        "order": {
            "id": "2",
            "orderiems": [
                {
                    "partcode": "A",
                    "qty": "4"
                },
                {
                    "partcode": "B",
                    "qty": "4"
                },
                {
                    "partcode": "A",
                    "qty": "4"
                }
            ]
        }
    },
    {
        "order": {
            "id": "3",
            "orderiems": [
                {
                    "partcode": "A",
                    "qty": "1"
                }
            ]
        }
    }
]

我的查询是

SELECT order from order
JOIN items in order.orderitem
WHERE item.partcode = '<mypartcode>
  AND item.qty > 1

现在,这种工作并返回我的订单,但它正在返回

  • id:1
  • id:2
  • id: 2 重复

因为 id: 2 有两个相同的项目.... id: 3 被排除在外,因为它只有 1 个项目

在普通的 SQL Server SQL 中,我只需要

SELECT *
  from Orders o 
 where exists (select 1 
                 from OrderItems oi 
                where oi.ordID = o.ID 
                  and oi.partcode = 'A'
                  and oi.qty > 1)

请问我怎样才能停止复制

请注意,以上是手工制作的表示,以简化问题,因为我实际处理的文档模型非常大

【问题讨论】:

  • 您能否更新您当前的查询。我正在尝试针对您的数据集运行它,但它没有返回您所描述的内容。
  • 完成@NickChapsas - 请参阅问题底部的注释(粗体)
  • 您是否尝试过使用 distinct 关键字?顺便说一句,我仍然无法使用您的查询。
  • 好吧,我会被吹的...... DISTINCT 似乎工作,没想到它会在一个完整的文档(非常大)文档上...... thnx
  • 写了一个答案。请接受。谢谢。

标签: azure-cosmosdb azure-cosmosdb-sqlapi


【解决方案1】:

Cosmos DB 现在支持 DISTINCT 关键字,它实际上适用于文档用例,例如您的用例。

【讨论】:

    猜你喜欢
    • 2017-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多