通过不使用猫鼬填充进行分组

时间:2019-02-24 11:35:07

标签: node.js mongodb mongoose

我无法在.populate({})中填写组的字段,我尝试了很多次,但是我不明白问题所在:

Company.find({
    _id: req.body.idCompany,
  })
    .populate({
      path: 'listAgencys',
      model: 'Agency',
      match: {
        idClient: req.body.idClient,
        createdAt: {
          $gte: new Date(req.body.startDate),
          $lte: new Date(req.body.endDate)
        }
      },
       group: {
        _id: {
         subscriptionType: '$subscriptionName',
          year: { $year: '$createdAt' },
          month: { $month: '$createdAt' },
          month: {
            $let: {
              vars: {
                monthsInString: [, 'Jan.', 'Fev.', 'Mars', ......]
              },
              in: {
                $arrayElemAt: ['$$monthsInString', { $month: '$createdAt' }]
              }
            }
          }
        },
        countLines: { $sum: 1 },
      } ,
        group: {
          _id: "$_id.subscriptionType",
          info: {
            $push: {
              year: "$_id.year",
              month: "$_id.month",
              allLines: "$countLines",

            }
          },
        }     
    })
    .exec();

模式:

const companySchema = new mongoose.Schema({
  _id: mongoose.Schema.Types.ObjectId,
  name: {
    type: String,
    trim: true,
  },
  listAgencys: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Agency",
    }
  ]
});

module.exports = mongoose.model("Company", companySchema);


--

const agencySchema = new mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    idClient: {
        type: Number,
        trim: true,
        //unique: true,
    },
    adress: {
        type: String,
        trim: true,
        lowercase: true,
    },
    createdAt: {
        type: Date,
        default: Date.now()
      },
    listOfSubscribers: [{
        numberSubscriber: {
            type: Number,
        },

        numberPhone: {
            type: Number,
        },
        subscriptionName: {
            type: String
        },
    }], 
});

module.exports = mongoose.model("Agency", agencySchema);

参数示例;

{
    "idCompany": "5c71ba1c1376b034f8dbceb6",
    "startDate":"2019-02-23",
    "endDate":"2019-03-31", 
    "idClient" : "021378009"
}

我想根据参数中传递的idAgency和idCompany显示每月的订阅者数量和subscriptionType。

Edit1,汇总:

Company.aggregate(
    [
     { $unwind: "$listAgencys" },
      {
        $match: {
        _id: req.body.idCompany,
        idClient: req.body.idClient,
        createdAt: { 
           "istAgencys.createdAt": {
              $gte: new Date(req.body.startDate),
              $lte: new Date(req.body.endDate)
           }
        }
      },
       {
        $group: {
        _id: {
         subscriptionType: '$listAgencys.subscriptionName',
          year: { $year: '$createdAt' },
          month: { $month: '$createdAt' },
          month: {
            $let: {
              vars: {
                monthsInString: [, 'Jan.', 'Fev.', 'Mars', ......]
              },
              in: {
                $arrayElemAt: ['$$monthsInString', { $month: '$createdAt' }]
              }
            }
          }
        }
        countLines: { $sum: 1 },
      } ,
     {
       $group: {
          _id: "$_id.subscriptionType",
          info: {
            $push: {
              year: "$_id.year",
              month: "$_id.month",
              allLines: "$countLines",

            }
          }
        } 
      }    
    ])

编辑结果1:

{
   success: true,
   datalines : []
}

输出示例:

{
    "success": true,
    "datalines": [
     { 
       "idClient": 0213400892124
        {
            "_id": {
                "subscriptionType": "ADL",
                "year": 2019,
                "month" : "Fev." 
            },
            "allLines": 3,
        },
        {
            "_id": {
                "subscriptionType": "Lines",
                "year": 2019,
                "month" : "Jan." 
            },
            "allLines": 10,

        },
        {
            "_id": {
                "subscriptionType": "Others",
                "year": 2019,
                "month" : "Mars" 
            },
            "allLines": 35,
        }
   },
  {
   "idClient": 78450012365
    .........
  }
    ]
}

先谢谢您

0 个答案:

没有答案