MongoDB
 sql >> Datenbank >  >> NoSQL >> MongoDB

MongoDB:Wie multipliziert man ein Feld, das nur in $project erscheint?

Bitte verwenden Sie das Schlüsselwort addFields, nachdem wir den Schlüssel im Projekt .

verwenden können
 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"]
            }
          }
        }
      ]