将MongoDB聚合转换为ArangoDB COLLECT

时间:2018-06-06 17:07:45

标签: arangodb aql

我正在将数据从Mongo迁移到Arango,我需要重现$ group聚合。我已成功复制了结果,但我担心我的方法可能不是最佳的。 AQL能改进吗?

我有一组如下所示的数据:

{
    "_id" : ObjectId("5b17f9d85b2c1998598f054e"),
    "department" : [ 
        "Sales", 
        "Marketing"
    ],
    "region" : [ 
        "US", 
        "UK"
    ]
}

{
    "_id" : ObjectId("5b1808145b2c1998598f054f"),
    "department" : [ 
        "Sales", 
        "Marketing"
    ],
    "region" : [ 
        "US", 
        "UK"
    ]
}

{
    "_id" : ObjectId("5b18083c5b2c1998598f0550"),
    "department" : "Development",
    "region" : "Europe"
}

{
    "_id" : ObjectId("5b1809a75b2c1998598f0551"),
    "department" : "Sales"
}

注意,值可以是字符串,数组或不存在

在Mongo中,我使用以下代码来聚合数据:

db.test.aggregate([
{
    $unwind:{
        path:"$department",
        preserveNullAndEmptyArrays: true
    }
},
{
    $unwind:{
        path:"$region",
        preserveNullAndEmptyArrays: true
    }
},
{
    $group:{
        _id:{
            department:{ $ifNull: [ "$department", "null" ] },
            region:{ $ifNull: [ "$region", "null" ] },
        },
        count:{$sum:1}
    }
}
])

在Arango我正在使用以下AQL:

FOR i IN test
    LET FIELD1=(FOR a IN APPEND([],NOT_NULL(i.department,"null")) RETURN a)
    LET FIELD2=(FOR a IN APPEND([],NOT_NULL(i.region,"null")) RETURN a)

    FOR f1 IN FIELD1
        FOR f2 IN FIELD2
            COLLECT id={department:f1,region:f2} WITH COUNT INTO counter

            RETURN {_id:id,count:counter}

编辑: APPEND用于将字符串值转换为数组

两者都产生看起来像这样的结果;

{
    "_id" : {
        "department" : "Marketing",
        "region" : "US"
    },
    "count" : 2.0
}

{
    "_id" : {
        "department" : "Development",
        "region" : "Europe"
    },
    "count" : 1.0
}

{
    "_id" : {
        "department" : "Sales",
        "region" : "null"
    },
    "count" : 1.0
}

{
    "_id" : {
        "department" : "Marketing",
        "region" : "UK"
    },
    "count" : 2.0
}

{
    "_id" : {
        "department" : "Sales",
        "region" : "UK"
    },
    "count" : 2.0
}

{
    "_id" : {
        "department" : "Sales",
        "region" : "US"
    },
    "count" : 2.0
}

1 个答案:

答案 0 :(得分:2)

你的方法似乎没问题。我建议使用TO_ARRAY()代替APPEND(),以便更容易理解。

两个函数都跳过空值,因此不可避免地提供一些占位符,或者显式测试 null 并返回一个带有 null 值的数组(或者其他最好的值)对你而言):

FOR doc IN test
    FOR field1 IN doc.department == null ? [ null ] : TO_ARRAY(doc.department)
    FOR field2 IN doc.region == null ? [ null ] : TO_ARRAY(doc.region)
    COLLECT department = field1, region = field2
    WITH COUNT INTO count
        RETURN { _id: { department, region }, count }

收藏测试

[
  {
    "_key": "5b17f9d85b2c1998598f054e",
    "department": [
      "Sales",
      "Marketing"
    ],
    "region": [
      "US",
      "UK"
    ]
  },
  {
    "_key": "5b18083c5b2c1998598f0550",
    "department": "Development",
    "region": "Europe"
  },
  {
    "_key": "5b1808145b2c1998598f054f",
    "department": [
      "Sales",
      "Marketing"
    ],
    "region": [
      "US",
      "UK"
    ]
  },
  {
    "_key": "5b1809a75b2c1998598f0551",
    "department": "Sales"
  }
]

结果:

[
  {
    "_id": {
      "department": "Development",
      "region": "Europe"
    },
    "count": 1
  },
  {
    "_id": {
      "department": "Marketing",
      "region": "UK"
    },
    "count": 2
  },
  {
    "_id": {
      "department": "Marketing",
      "region": "US"
    },
    "count": 2
  },
  {
    "_id": {
      "department": "Sales",
      "region": null
    },
    "count": 1
  },
  {
    "_id": {
      "department": "Sales",
      "region": "UK"
    },
    "count": 2
  },
  {
    "_id": {
      "department": "Sales",
      "region": "US"
    },
    "count": 2
  }
]