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

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

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

问题

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

  1. const LEAD_PRICE = ....; // 不重要
  2. Client.aggregate(
  3. [
  4. {
  5. $lookup: {
  6. from: "clientboughtleads",
  7. localField: "_id",
  8. foreignField: "Client",
  9. as: "ClientLeads"
  10. }
  11. },
  12. {
  13. $project: {
  14. ClientId: "$_id",
  15. RegistrationDate: "$date",
  16. TotalPurchasedLeads: {
  17. $size: "$ClientLeads"
  18. },
  19. IncomeFromClient: { // 这个总是未定义
  20. $multiply: [LEAD_PRICE / 2, "$TotalPurchasedLeads"]
  21. }
  22. }
  23. }
  24. ]
  25. )

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

  1. [
  2. {
  3. _id: 5e0e43ae82a71e0017dccb20,
  4. ClientId: 5e0e43ae82a71e0017dccb20,
  5. RegistrationDate: 2020-01-02T21:25:34.000Z,
  6. TotalPurchasedLeads: 4,
  7. IncomeFromClient: null // 这个未计算
  8. }
  9. ]

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

英文:

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

  1. const LEAD_PRICE = ....; // doesn't matter
  2. Client.aggregate(
  3. [
  4. {
  5. $lookup: {
  6. from: "clientboughtleads",
  7. localField: "_id",
  8. foreignField: "Client",
  9. as: "ClientLeads"
  10. }
  11. },
  12. {
  13. $project: {
  14. ClientId: "$_id",
  15. RegistrationDate: "$date",
  16. TotalPurchasedLeads: {
  17. $size: "$ClientLeads"
  18. },
  19. IncomeFromClient: { // This one is always undefined
  20. $multiply: [LEAD_PRICE / 2, "$TotalPurchasedLeads"]
  21. }
  22. }
  23. }
  24. ]

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

  1. [
  2. [0] {
  3. [0] _id: 5e0e43ae82a71e0017dccb20,
  4. [0] ClientId: 5e0e43ae82a71e0017dccb20,
  5. [0] RegistrationDate: 2020-01-02T21:25:34.000Z,
  6. [0] TotalPurchasedLeads: 4,
  7. [0] IncomeFromClient: null // This one is not calculated
  8. [0] }
  9. [0] ]

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

答案1

得分: 1

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

  1. const LEAD_PRICE = ....; // doesn't matter
  2. Client.aggregate(
  3. [
  4. {
  5. $lookup: {
  6. from: "clientboughtleads",
  7. localField: "_id",
  8. foreignField: "Client",
  9. as: "ClientLeads"
  10. }
  11. },
  12. {
  13. $addFields: {
  14. TotalPurchasedLeads: {
  15. $size: "$ClientLeads"
  16. }
  17. }
  18. },
  19. {
  20. $project: {
  21. ClientId: "$_id",
  22. RegistrationDate: "$date",
  23. TotalPurchasedLeads: 1,
  24. IncomeFromClient: { // This one is always undefined
  25. $multiply: [LEAD_PRICE / 2, "$TotalPurchasedLeads"]
  26. }
  27. }
  28. }
  29. ]
  30. )
英文:

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

  1. const LEAD_PRICE = ....; // doesn't matter
  2. Client.aggregate(
  3. [
  4. {
  5. $lookup: {
  6. from: "clientboughtleads",
  7. localField: "_id",
  8. foreignField: "Client",
  9. as: "ClientLeads"
  10. }
  11. },
  12. {
  13. $addFields: {
  14. TotalPurchasedLeads: {
  15. $size: "$ClientLeads"
  16. }
  17. }
  18. },
  19. {
  20. $project: {
  21. ClientId: "$_id",
  22. RegistrationDate: "$date",
  23. TotalPurchasedLeads:1,
  24. IncomeFromClient: { // This one is always undefined
  25. $multiply: [LEAD_PRICE / 2, "$TotalPurchasedLeads"]
  26. }
  27. }
  28. }
  29. ]

答案2

得分: 1

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

  1. {
  2. "$project": {
  3. "ClientId": "$_id",
  4. "RegistrationDate": "$date",
  5. "TotalPurchasedLeads": {
  6. "$size": "$ClientLeads"
  7. },
  8. "IncomeFromClient": {
  9. "$multiply": [LEAD_PRICE / 2, { "$size": "$ClientLeads" }]
  10. }
  11. }
  12. }
英文:

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

  1. {
  2. "$project": {
  3. "ClientId": "$_id",
  4. "RegistrationDate": "$date",
  5. "TotalPurchasedLeads": {
  6. "$size": "$ClientLeads"
  7. },
  8. "IncomeFromClient": {
  9. "$multiply": [LEAD_PRICE / 2, { "$size": "$ClientLeads" }]
  10. }
  11. }
  12. }

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:

确定