MongoDB无法从_id投射字符串值

时间:2021-12-14 17:56:15

I have an aggregate to pull metrics from a collection of "share" actions. These metrics are grouped by day and the platform(twitter, pintrest, etc). Each stage works fine except when it comes to formatting the output in a $project stage, I cannot seem to get the platform string to project in my output. I see it in the _id still, however I cannot get it to project to the other field.

我有一个聚合来从一组“共享”操作中提取指标。这些指标按日和平台(twitter,pintrest等)分组。每个阶段工作正常,除非在$ project阶段格式化输出,我似乎无法在我的输出中获得平台字符串。我仍然在_id中看到它,但是我不能让它投射到另一个领域。

The aggregation is as follows:

汇总如下:

{
  $unwind: "$platforms"
},
{
  $project: {
    day: {$dayOfMonth: "$created_at"},
    month: {$month: "$created_at"},
    year: {$year: "$created_at"},
    platforms: 1
  }
},
{
  $group: {
    _id: {
      platorms: "$platforms",
      day: "$day",
      month: "$month",
      year: "$year"
    },
    count: {$sum: 1}
  }
},
{
  $project: {
    day: "$_id.day",
    month: "$_id.month",
    year: "$_id.year",
    platform: "$_id.platforms",
    count: 1
  }
}

The output of the following is:

以下输出是:

{
"result" : [ 
    {
        "_id" : {
            "platorms" : "EMAIL",
            "day" : 28,
            "month" : 4,
            "year" : 2015
        },
        "count" : 1,
        "day" : 28,
        "month" : 4,
        "year" : 2015
    }, 
    {
        "_id" : {
            "platorms" : "EMAIL",
            "day" : 27,
            "month" : 4,
            "year" : 2015
        },
        "count" : 10,
        "day" : 27,
        "month" : 4,
        "year" : 2015
    }, 
    {
        "_id" : {
            "platorms" : "SMS",
            "day" : 27,
            "month" : 4,
            "year" : 2015
        },
        "count" : 1,
        "day" : 27,
        "month" : 4,
        "year" : 2015
    }, 
    {
        "_id" : {
            "platorms" : "FACEBOOK",
            "day" : 27,
            "month" : 4,
            "year" : 2015
        },
        "count" : 1,
        "day" : 27,
        "month" : 4,
        "year" : 2015
    }, ...

The integers for day, month, and year project fine, however platforms does not.

日,月和年的整数项目很好,但平台没有。

1 个解决方案

#1


This is because in your $group stage you have compound _id with platorms: "$platforms" (missing f in field name) and you are projecting the $_id.platforms in your last $project stage and since such field doesn't exist you will not get any result. You can also exclude the _id field using _id: 0.

这是因为在$ group阶段你有复合_id与platorms:“$ platforms”(在字段名称中缺少f)并且你在上一个$ project阶段投射$ _id.platforms,因为这样的字段不存在你不会得到任何结果。您还可以使用_id:0排除_id字段。

Change your $group stage to:

将$ group阶段更改为:

{
  $group: {
    _id: {
      platforms: "$platforms",
      day: "$day",
      month: "$month",
      year: "$year"
    },
    count: {$sum: 1}
  }
}

#1


This is because in your $group stage you have compound _id with platorms: "$platforms" (missing f in field name) and you are projecting the $_id.platforms in your last $project stage and since such field doesn't exist you will not get any result. You can also exclude the _id field using _id: 0.

这是因为在$ group阶段你有复合_id与platorms:“$ platforms”(在字段名称中缺少f)并且你在上一个$ project阶段投射$ _id.platforms,因为这样的字段不存在你不会得到任何结果。您还可以使用_id:0排除_id字段。

Change your $group stage to:

将$ group阶段更改为:

{
  $group: {
    _id: {
      platforms: "$platforms",
      day: "$day",
      month: "$month",
      year: "$year"
    },
    count: {$sum: 1}
  }
}