我的文档中有这样的数据结构(请注意,为简洁起见,这是简化的):
{
"id": "c1c1c1c1-c1c1-c1c1-c1c1-c1c1c1c1c1c1",
"name": "Bruce Banner",
"accountId": "a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1",
"contributors": [{
"accountId": "a2a2a2a2-a2a2-a2a2-a2a2-a2a2a2a2a2a2",
"type": "Foo"
},{
"accountId": "a3a3a3a3-a3a3-a3a3-a3a3-a3a3a3a3a3a3",
"type": "Bar"
}]
},
{
"id": "c2c2c2c2-c2c2-c2c2-c2c2-c2c2c2c2c2c2",
"name": "Tony Stark",
"accountId": "a2a2a2a2-a2a2-a2a2-a2a2-a2a2a2a2a2a2",
"contributors": [{
"accountId": "a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1",
"type": "Fizz"
}]
},
我正在尝试编写一个查询来检索提供accountId
的文档在父记录上或contributors
数组内:
SELECT e.id, e.accountId, e.name
FROM Entitity e
JOIN co IN e.contributors
WHERE e.accountId = 'a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1'
OR co.accountId = 'a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1'
结果:
[{
"id": "c1c1c1c1-c1c1-c1c1-c1c1-c1c1c1c1c1c1",
"accountId": "a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1",
"name": "Bruce Banner"
},{
"id": "c1c1c1c1-c1c1-c1c1-c1c1-c1c1c1c1c1c1",
"accountId": "a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1",
"name": "Bruce Banner"
},{
"id": "c2c2c2c2-c2c2-c2c2-c2c2-c2c2c2c2c2c2",
"accountId": "a2a2a2a2-a2a2-a2a2-a2a2-a2a2a2a2a2a2",
"name": "Tony Stark"
}]
如您所见,第一个实体 ( Bruce Banner
) 是重复的。如果我删除该JOIN
子句,它会正常工作。谁能告诉我这是为什么,以及如何避免重复?
编辑- 为清楚起见,这是我的预期回复:
[{
"id": "c1c1c1c1-c1c1-c1c1-c1c1-c1c1c1c1c1c1",
"accountId": "a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1",
"name": "Bruce Banner"
},{
"id": "c2c2c2c2-c2c2-c2c2-c2c2-c2c2c2c2c2c2",
"accountId": "a2a2a2a2-a2a2-a2a2-a2a2-a2a2a2a2a2a2",
"name": "Tony Stark"
}]