根据项目状态查询 Azure Cosmos DB 集合
Query Azure Cosmos DB collection based on item status
在 Azure 上的 Azure Cosmos DB 中,我有一个具有以下结构的集合:
[
{
"id": "1",
"status": "wait",
"order": {
"orderId": "P1000",
"orderPositionId": "1"
}
},
{
"id": "2",
"status": "wait",
"order":{
"orderId": "P1000",
"orderPositionId": "2"
}
},
{
"id": "3",
"status": "start",
"order":{
"orderId": "P1000",
"orderPositionId": "1"
}
}
]
orderId 和 orderPositionId 的组合是唯一的。
我想查询所有状态为 "wait" 的项目,但不存在状态为 "start" 的项目。
对于上面的例子,查询的期望结果是:
[
{
"id": "2",
"status": "wait",
"order":{
"orderId": "P1000",
"orderPositionId": "2"
}
}
]
我的想法是这样做的(伪代码)
SELECT *FROM c
WHERE
c.status="wait"
AND
EXISTS(SELECT * FROM c
WHERE c.order.orderId = c.order.orderId **(id of current item)** AND
c.order.orderPositionId=c.order.orderPositionId **(id of current item)** AND
c.status="start") = false
我知道这样的想法行不通。
我是 Cosmos DB 的新手,也是 SQL API 的新手,我无法找到可行的解决方案。感谢您提供有关如何解决此问题的任何提示。
谢谢,如果您需要更多信息,请告诉我。
But my goal is to drop all wait items which have a corresponding start
item.
所以,根据这个,如果有2个文档:
{
"id": "1",
"status": "wait",
"order": {
"orderId": "P1000",
"orderPositionId": "1"
}
},
{
"id": "3",
"status": "start",
"order":{
"orderId": "P1000",
"orderPositionId": "1"
}
}
他们有相同的:
"order":{
"orderId": "P1000",
"orderPositionId": "1"
}
那么你需要从 wait
家族中删除这样的 wait
一个。
根据我的大量测试,我无法使用 array_contains method.So 的子查询,我必须使用 2 sql 来实现您的要求。
首先sql查询所有start
项:
SELECT c['order'].orderId,c['order'].orderPositionId FROM c
where c.status = 'start'
你会得到数组结果:
然后在 long sql:
下方使用该数组
select c.id,c.status,c['order']
from c
where c.status = 'wait' and not EXISTS
(select value c from c
where c.status = 'wait' and arraycontains(
[
{
"orderId": "P1000",
"orderPositionId": "1"
}
],{"orderId": c['order'].orderId,"orderPositionId":c['order'].orderPositionId},true))
你可以得到输出:
在 Azure 上的 Azure Cosmos DB 中,我有一个具有以下结构的集合:
[
{
"id": "1",
"status": "wait",
"order": {
"orderId": "P1000",
"orderPositionId": "1"
}
},
{
"id": "2",
"status": "wait",
"order":{
"orderId": "P1000",
"orderPositionId": "2"
}
},
{
"id": "3",
"status": "start",
"order":{
"orderId": "P1000",
"orderPositionId": "1"
}
}
]
orderId 和 orderPositionId 的组合是唯一的。
我想查询所有状态为 "wait" 的项目,但不存在状态为 "start" 的项目。
对于上面的例子,查询的期望结果是:
[
{
"id": "2",
"status": "wait",
"order":{
"orderId": "P1000",
"orderPositionId": "2"
}
}
]
我的想法是这样做的(伪代码)
SELECT *FROM c
WHERE
c.status="wait"
AND
EXISTS(SELECT * FROM c
WHERE c.order.orderId = c.order.orderId **(id of current item)** AND
c.order.orderPositionId=c.order.orderPositionId **(id of current item)** AND
c.status="start") = false
我知道这样的想法行不通。 我是 Cosmos DB 的新手,也是 SQL API 的新手,我无法找到可行的解决方案。感谢您提供有关如何解决此问题的任何提示。
谢谢,如果您需要更多信息,请告诉我。
But my goal is to drop all wait items which have a corresponding start item.
所以,根据这个,如果有2个文档:
{
"id": "1",
"status": "wait",
"order": {
"orderId": "P1000",
"orderPositionId": "1"
}
},
{
"id": "3",
"status": "start",
"order":{
"orderId": "P1000",
"orderPositionId": "1"
}
}
他们有相同的:
"order":{
"orderId": "P1000",
"orderPositionId": "1"
}
那么你需要从 wait
家族中删除这样的 wait
一个。
根据我的大量测试,我无法使用 array_contains method.So 的子查询,我必须使用 2 sql 来实现您的要求。
首先sql查询所有start
项:
SELECT c['order'].orderId,c['order'].orderPositionId FROM c
where c.status = 'start'
你会得到数组结果:
然后在 long sql:
下方使用该数组select c.id,c.status,c['order']
from c
where c.status = 'wait' and not EXISTS
(select value c from c
where c.status = 'wait' and arraycontains(
[
{
"orderId": "P1000",
"orderPositionId": "1"
}
],{"orderId": c['order'].orderId,"orderPositionId":c['order'].orderPositionId},true))
你可以得到输出: