Mongodb一次展开两个阵列

时间:2015-03-30 10:00:43

标签: mongodb aggregation-framework

我有一个集合,可以有多个标签和多个检测日期对应于标签,如下所示:

{
    "shop"=>"my_shop",
    "date"=>[
              [2015-01-03 00:00:00 UTC, 2015-01-10 00:00:00 UTC],
              [2015-01-10 00:00:00 UTC]
            ], 
    "tag"=>["test_tag1", "test_tag2"]
}

" date"中的每个数组;对应一个标签,所以只需展开两次就会失去这场比赛。

我想像这样保持这场比赛:

{
    "shop"=>"my_shop",
    "date"=>2015-01-03 00:00:00 UTC, 
    "tag"=>"test_tag1"
},
{
    "shop"=>"my_shop",
    "date"=>2015-01-10 00:00:00 UTC, 
    "tag"=>"test_tag1"
},
{
    "shop"=>"my_shop",
    "date"=>2015-01-10 00:00:00 UTC, 
    "tag"=>"test_tag2"
}

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:2)

不,你不能这样做。您的文档设计不正确。如果你像下面那样调整你的收藏,你可能会得到你想要的结果。

{
    "shop": "my_shop",
    "tags_with_date": [
        {
            "tag": "test_tag1",
            "date": [
                2015-01-0300: 00: 00UTC,
                2015-01-1000: 00: 00UTC
            ]
        },
        {
            "tag": "test_tag2",
            "date": [
                2015-01-1000: 00: 00UTC
            ]
        }
    ]
}