gpt4 book ai didi

azure-cosmosdb - 从 CosmosDB 中文档嵌套数组中的对象中选择值

转载 作者:行者123 更新时间:2023-12-01 09:44:52 25 4
gpt4 key购买 nike

想象一下我们有一个这样的集合(示例取自 https://www.documentdb.com/sql/demo )

{
"_id" : "19015",
"description" : "Snacks, granola bars, hard, plain",
"servings" : [
{
"amount" : 1,
"description" : "bar",
"weightInGrams" : 21
},
{
"amount" : 1,
"description" : "bar (1 oz)",
"weightInGrams" : 28
},
{
"amount" : 1,
"description" : "bar",
"weightInGrams" : 25
}
]
}

我如何在 SQL api 中查询 CosmosDB 以获得这样的结果?
{
"_id" : "19015",
"servings" : [
{
"description" : "bar"
},
{
"description" : "bar (1 oz)"
},
{
"description" : "bar"
}
]
}

在 MongoDB 我会使用这样的查询
db.getCollection('food').find({id: '19015'}, {'servings.description' : 1})

用连接等尝试了多个场景
SELECT 
food.id,
food.servings.description
FROM food
WHERE food.id = "19015"

或者
SELECT 
food.id,
[{
description: food.servings[0].description
}] AS servings
FROM food
WHERE food.id = "19015"
  • 而不是 [0]我试过 [][$]但不起作用

  • 有人知道我如何以简单的方式解决这个问题吗?

    最佳答案

    您可以使用 ARRAY(subquery) 表达式来实现这一点。这是查询:

    SELECT
    food.id,
    ARRAY(SELECT serving.description FROM serving IN food.servings) AS servings
    FROM food
    WHERE food.id = "19015"

    关于azure-cosmosdb - 从 CosmosDB 中文档嵌套数组中的对象中选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51441547/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com