Добавить документы из другой коллекции (B) в совокупность коллекции (A)? - PullRequest
0 голосов
/ 05 января 2020

У меня есть следующий конвейер:

EightWeekGamePlan.aggregate(
  [
    { $sort: { SupplierGeneralId: 1 } },

    {
      $group: {
        _id: {
          SupplierGeneralId: "$SupplierGeneralId",
          month: { $month: "$InsertDate" },
          year: { $year: "$InsertDate" }
        },
        LeadsUploaded: {
          $push: {
            LeadId: "$LeadId"
          }
        },
        StoppedLeads: {
          $push: {
            $cond: [
              { $eq: ["$Status", 3] },
              { LeadId: "$LeadId" },
              null
            ]
          }
        },
        Sells: { $sum: "$TotalClaimsLeftToBeClaimedByClientType" }
      }
    },

    {
      $project: {
        Sells: "$Sells",
        LeadsUploaded: {
          $reduce: {
            input: { $setUnion: "$LeadsUploaded.LeadId" },
            initialValue: [],
            in: {
              $concatArrays: [
                "$$value",
                [
                  {
                    Lead_ID: "$$this"
                  }
                ]
              ]
            }
          }
        },
        StoppedLeads: {
          $reduce: {
            input: { $setUnion: "$StoppedLeads.LeadId" },
            initialValue: [],
            in: {
              $concatArrays: [
                "$$value",
                [
                  {
                    Lead_ID: "$$this"
                  }
                ]
              ]
            }
          }
        }
      }
    },
    {
      $project: {
        SupplierGeneralId: {
          $concat: [
            { $substr: ["SU", 0, -1] },
            "-",
            { $substr: ["$_id.SupplierGeneralId", 0, -1] }
          ]
        },
        Sells: "$Sells",
        Month: {
          $concat: [
            { $substr: ["$_id.month", 0, -1] },
            "/",
            { $substr: ["$_id.year", 0, -1] }
          ]
        },
        TotalLeadsUploaded: {
          $size: "$LeadsUploaded"
        },
        TotalIncomeFromSells: { $multiply: [LEAD_PRICE, "$Sells"] },
        SupplierIncome: { $multiply: [LEAD_PRICE / 2, "$Sells"] },
        StoppedLeads: {
          $size: "$StoppedLeads"
        }
      }
    }
    ]

Я хочу добавить коллекцию документов из другой коллекции, единственное, что соединяет ее с конвейером агрегации, указанным выше, это SupplierGeneralId.

Схема:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const LeadsNotValidSchema = new Schema({
  supplier: {
    type: Schema.Types.ObjectId,
    ref: "suppliers",
    required: false
  },
  PackageId: {
    type: String,
    required: true
  },
  SupplierGeneralId: {
    type: String,
    required: true
  },
  PhoneNumberMasque: {
    type: String,
    required: true
  },
  BusinessName: {
    type: String,
    required: true
  },
  Duplicate: {
    type: Boolean,
    required: false
  },
  // Specifies if there is a problem with one of the required fields
  // Either one of them is not in the correct format or even missing
  Valid: {
    type: Boolean,
    required: false
  },
  LeadId: {
    type: String,
    required: true
  },
  City: {
    type: String,
    required: true
  },
  Rooms: {
    type: String,
    default: 999
  },
  Status: {
    type: Number,
    require: true
  },
  PublishDate: {
    type: Date
  }
});

module.exports = LeadsNotValid = mongoose.model(
  "leadsNotValid",
  LeadsNotValidSchema
);

Я хочу использовать то же самое предложение where, что и в приведенном выше предложении группы:

1) SupplierGeneralId будет таким же

2) PublishDate будет соответствовать одному в группе

   SupplierGeneralId: "$SupplierGeneralId",
              month: { $month: "$InsertDate" },
              year: { $year: "$InsertDate"

Можно ли получить документы из leadsNotValid без $lookup?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...