我可以将MongoDB Document转换为在Array中包含一个Array吗?

时间:2016-04-04 10:27:40

标签: mongodb mongodb-query

我正在尝试转换看起来像这样的文档

{ "_id": 1,
  "created_at": DateTimeObject,
  "daily": [{ "impressions":13,
              "clicks": 13,
              "day": "2015-01-01" },
            { "impressions":15,
              "clicks": 15,
              "day": "2015-01-02" },
            { "impressions":20,
              "clicks": 20,
              "day": "2015-01-03" }]
}

{ "_id": 1,
  "impressions": [["2015-01-01", 13], 
                  ["2015-01-02", 15], 
                  ["2015-01-03", 20]],
  "clicks": [["2015-01-01", 13], 
             ["2015-01-02", 15], 
             ["2015-01-03", 20]] }

目前正在使用展开,我可以使用以下格式获取数据

{ "_id": 1,
  "impressions": [{ "date": "2015-01-01", "value":13], 
                  {"date":"2015-01-02", "value":15}, 
                  {"date":"2015-01-03", "value":20}],
  "clicks": [{"date": "2015-01-01", "value":13}, 
             {"date": "2015-01-01", "value":15}, 
             {"date": "2015-01-01", "value":20}] }

我不想在数据库之外执行此操作,因为需要从类似文档生成大约150 - 200个折线图,并且更愿意进行转​​换。

编辑这可以通过map reduce实现吗?

1 个答案:

答案 0 :(得分:0)

Mongodb无法支持内部阵列。你需要用unwind和group编写。