MongoDB:如何对仅在$project中出现的字段进行乘法操作?

huangapple go评论81阅读模式
英文:

MongoDB : How to multiply a field that appears only in $project?

问题

我正在使用以下方式连接两个集合($lookup):

const LEAD_PRICE = ....; // 不重要
Client.aggregate(
    [
        {
            $lookup: {
                from: "clientboughtleads",
                localField: "_id",
                foreignField: "Client",
                as: "ClientLeads"
            }
        },

        {
            $project: {
                ClientId: "$_id",
                RegistrationDate: "$date",
                TotalPurchasedLeads: {
                    $size: "$ClientLeads"
                },
                IncomeFromClient: { // 这个总是未定义
                    $multiply: [LEAD_PRICE / 2, "$TotalPurchasedLeads"]
                }
            }
        }
    ]
)

我想通过常量和一个已计算的字段TotalPurchasedLeads的乘积来计算IncomeFromClient,但它返回了NULL:

[
    {
        _id: 5e0e43ae82a71e0017dccb20,
        ClientId: 5e0e43ae82a71e0017dccb20,
        RegistrationDate: 2020-01-02T21:25:34.000Z,
        TotalPurchasedLeads: 4,
        IncomeFromClient: null // 这个未计算
    }
]

在计算一个被$project的字段时,如何获取$lookup集合的大小?

英文:

I'm joining ($lookup) two collection the following way :

  const LEAD_PRICE  = ....; // doesn't matter 
  Client.aggregate(
      [
        {
          $lookup: {
            from: "clientboughtleads",
            localField: "_id",
            foreignField: "Client",
            as: "ClientLeads"
          }
        },

        {
          $project: {
            ClientId: "$_id",
            RegistrationDate: "$date",
            TotalPurchasedLeads: {
              $size: "$ClientLeads"
            },
            IncomeFromClient: {     // This one is always undefined
              $multiply: [LEAD_PRICE / 2, "$TotalPurchasedLeads"]
            }
          }
        }
      ]

I want to calculate IncomeFromClient by the mul of a const and a field that is been also calculated - TotalPurchasedLeads , however it returns NULL :

 [
[0]   {
[0]     _id: 5e0e43ae82a71e0017dccb20,
[0]     ClientId: 5e0e43ae82a71e0017dccb20,
[0]     RegistrationDate: 2020-01-02T21:25:34.000Z,
[0]     TotalPurchasedLeads: 4,
[0]     IncomeFromClient: null      // This one is not calculated 
[0]   }
[0] ]

How can I get the size of the $lookup collection when calculating a field that is being $projected ?

答案1

得分: 1

请在我们可以在项目中使用关键字"addFields"之后使用"key"。

const LEAD_PRICE = ....; // doesn't matter 
Client.aggregate(
    [
        {
            $lookup: {
                from: "clientboughtleads",
                localField: "_id",
                foreignField: "Client",
                as: "ClientLeads"
            }
        },
        {
            $addFields: {
                TotalPurchasedLeads: {
                    $size: "$ClientLeads"
                }
            }
        },
        {
            $project: {
                ClientId: "$_id",
                RegistrationDate: "$date",
                TotalPurchasedLeads: 1,
                IncomeFromClient: { // This one is always undefined
                    $multiply: [LEAD_PRICE / 2, "$TotalPurchasedLeads"]
                }
            }
        }
    ]
)
英文:

Please use addFields keyword after we can use key in project .

 const LEAD_PRICE  = ....; // doesn't matter 
  Client.aggregate(
      [
        {
          $lookup: {
            from: "clientboughtleads",
            localField: "_id",
            foreignField: "Client",
            as: "ClientLeads"
          }
        },
   {
     $addFields: {
       TotalPurchasedLeads: {
              $size: "$ClientLeads"
          }
     }
   },
        {
          $project: {
            ClientId: "$_id",
            RegistrationDate: "$date",
            TotalPurchasedLeads:1,
            IncomeFromClient: {     // This one is always undefined
              $multiply: [LEAD_PRICE / 2, "$TotalPurchasedLeads"]
            }
          }
        }
      ]

答案2

得分: 1

你不能在相同的 $project 阶段中使用 $project 的值。所以,改为使用其值

{
  "$project": {
    "ClientId": "$_id",
    "RegistrationDate": "$date",
    "TotalPurchasedLeads": {
      "$size": "$ClientLeads"
    },
    "IncomeFromClient": {
      "$multiply": [LEAD_PRICE / 2, { "$size": "$ClientLeads" }]
    }
  }
}
英文:

You cannot use the $projected value in the same $projection stage. So instead use its value

{
  "$project": {
    "ClientId": "$_id",
    "RegistrationDate": "$date",
    "TotalPurchasedLeads": {
      "$size": "$ClientLeads"
    },
    "IncomeFromClient": {
      "$multiply": [LEAD_PRICE / 2, { "$size": "$ClientLeads" }]
    }
  }
}

huangapple
  • 本文由 发表于 2020年1月3日 18:06:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定