且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

查询子对象中返回的重复项

更新时间:2023-02-13 15:54:53

我看到您正在尝试查询accountIdcontributorsaccountId是否等于某个值.

I see you are trying to query on whether the accountId or the contributors's accountId is equal to some value.

今天-您需要使用JOIN运算符执行叉积运算以查询JSON数组中的所有元素(注意:您不需要JOIN即可查询特定的数组索引,例如WHERE e.contributors[0].accountId = 'a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1').

Today - you need to use the JOIN operator to perform a cross-product in order to query across all elements within a JSON array (note: you do not need a JOIN to query on a specific array index, e.g. WHERE e.contributors[0].accountId = 'a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1').

在您的示例abpve中,您将不可避免地从查询中获得重复项.您将需要实现一些应用程序逻辑,以从查询结果中过滤出重复项.

In your example abpve, you will get duplicates from the query which is unavoidable. You will want to implement some application logic to filter duplicates out of the query result.

要更好地了解JOIN的行为(认为简单的叉积),请尝试从您要生成叉积的数组中添加一个字段(例如co.type):

To get a better picture in to the behavior of JOIN (think simple cross product), try adding a field from the array you are producing a cross product with (e.g. co.type):

SELECT e.id, e.accountId, e.name, co.type
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,
    type: Foo
}, {
    id: c1c1c1c1 - c1c1 - c1c1 - c1c1 - c1c1c1c1c1c1,
    accountId: a1a1a1a1 - a1a1 - a1a1 - a1a1 - a1a1a1a1a1a1,
    name: Bruce Banner,
    type: Bar
}, {
    id: c2c2c2c2 - c2c2 - c2c2 - c2c2 - c2c2c2c2c2c2,
    accountId: a2a2a2a2 - a2a2 - a2a2 - a2a2 - a2a2a2a2a2a2,
    name: Tony Stark,
    type: Fizz
}]

从结果中可以看到-每个子代(FooBarFizz)都将返回一条记录.那是因为每个数组元素都与指定的查询匹配.

As you can see from the results - a record is being returned for each of the children: Foo, Bar, and Fizz. That is because each of those array elements match the specified query.