[英]Arangodb groupby query multiple fields
I have a database of products in an arangodb collection in which a product has multiple sizes.我在 arangodb 集合中有一个产品数据库,其中一个产品有多种尺寸。 The issue is that for each size, the same product is repeated.
问题是对于每个尺寸,重复相同的产品。 But each product has a common group number.
但是每个产品都有一个共同的组号。 Like this:
像这样:
{"name": "product1", "description": "someDescription", size: 5,price: 12 groupNumber: 12}
{"name": "product1", "description": "someDescription", size: 15, price: 26, groupNumber: 12}
{"name": "product1", "description": "someDescription", size: 25, price: 84, groupNumber: 12}
{"name": "product1", "description": "someDescription", size: 35, price: 106, groupNumber: 12}
{"name": "product2", "description": "someDescription", size: 5, price: 12, groupNumber: 11}
{"name": "product2", "description": "someDescription", size: 15, price: 22, groupNumber: 11}
{"name": "product2", "description": "someDescription", size: 25, price: 32, groupNumber: 11}
{"name": "product2", "description": "someDescription", size: 35, price: 43, groupNumber: 11}
I have to now display the list of products(in a web page) but each product should appear only once with sizes and prices in an array for each product like this:我现在必须显示产品列表(在网页中),但每个产品应该只出现一次,每个产品的尺寸和价格在一个数组中,如下所示:
product1 someDescription sizes: 5,15,25,35, prices: 12,26,84,106
product2 someDescription sizes: 5,15,25,35, prices: 12,22,32,43
...
How do I do it?我该怎么做?
Ignoring the groupNumber
and grouping by name
, the query looks like this:忽略
groupNumber
并按name
分组,查询如下所示:
FOR p IN products
COLLECT description = p.description, name = p.name INTO groups
RETURN {
"name" : name,
"description": description,
"prices" : groups[*].p.price,
"sizes" : groups[*].p.size
}
Given your (corrected) example data, the query returns:鉴于您的(更正的)示例数据,查询返回:
[
{
"name": "product1",
"description": "someDescription",
"prices": [
12,
84,
106,
26
],
"sizes": [
5,
25,
35,
15
]
},
{
"name": "product2",
"description": "someDescription",
"prices": [
43,
32,
22,
12
],
"sizes": [
35,
25,
15,
5
]
}
]
The grouped values aren't sorted, but the positions of sizes and prices correspond, you can alleviate this fact to zip the values into a size-price map:分组的值没有排序,但大小和价格的位置相对应,您可以缓解这一事实,将值压缩到大小价格映射中:
FOR p IN products
COLLECT description = p.description, name = p.name INTO groups
RETURN {
"name" : name,
"description": description,
"size_price_map" : ZIP(groups[*].p.size, groups[*].p.price)
}
yielding:产生:
[
{
"name": "product1",
"description": "someDescription",
"size_price_map": {
"5": 12,
"15": 26,
"25": 84,
"35": 106
}
},
{
"name": "product2",
"description": "someDescription",
"size_price_map": {
"5": 12,
"15": 22,
"25": 32,
"35": 43
}
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.