Mongo 一次展开两个数组

Mongo unwind two arrays at once

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

{
    "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"
}

有办法吗?

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

{
    "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
            ]
        }
    ]
}