MongoDB聚合查找匹配在没有查找的情况下无法正常工作

时间:2019-11-24 00:33:05

标签: node.js mongodb express mongoose

可以帮忙吗?我正在尝试通过特定发布者的所有发布数据以及每个发布来汇总过去12个月的数据,以返回基于订阅类型的年度图表数据分析。

这是订户模型的快照:

  const SubscriberSchema = new Schema({
    publication: { type: Schema.Types.ObjectId, ref: "publicationcollection" },
    subType: { type: String }, // Print, Digital, Bundle
    subStartDate: { type: Date },
    subEndDate: { type: Date },
});

以下是读者(订户)集合的一些数据:

{
   _id: ObjectId("5dc14d3fc86c165ed48b6872"),
   publication: ObjectId("5d89db9d82273f1d18970deb"),
   subStartDate: "2019-11-20T00:00:00.000Z",
   subtype: "print"
},
{
   _id: ObjectId("5dc14d3fc86c165ed48b6871"),
   publication: ObjectId("5d89db9d82273f1d18970deb"),
   subStartDate: "2019-11-19T00:00:00.000Z",
   subtype: "print"
},
{
   _id: ObjectId("5dc14d3fc86c165ed48b6870"),
   publication: ObjectId("5d89db9d82273f1d18970deb"),
   subStartDate: "2019-11-18T00:00:00.000Z",
   subtype: "digital"
},
{
   _id: ObjectId("5dc14d3fc86c165ed48b6869"),
   publication: ObjectId("5d8b36c3148c1e5aec64662c"),
   subStartDate: "2019-11-19T00:00:00.000Z",
   subtype: "print"
}

发布模型具有许多字段,但是_id和user字段是以下查询中的唯一参考点。

以下是出版物集合的一些数据:

// Should use
{ "_id": {
        "$oid": "5d8b36c3148c1e5aec64662c"
    },
    "user": {
        "$oid": "5d24bbd89f09024590db9dcd"
    },
    "isDeleted": false
},
// Should use
{ "_id": {
        "$oid": "5d89db9d82273f1d18970deb"
    },
    "user": {
        "$oid": "5d24bbd89f09024590db9dcd"
    },
    "isDeleted": false
},
// Shouldn't use as deleted === true
{ "_id": {
        "$oid": "5d89db9d82273f1d18970dec"
    },
    "user": {
        "$oid": "5d24bbd89f09024590db9dcd"
    },
    "isDeleted": true
},
// Shouldn't use as different user ID
{ "_id": {
        "$oid": "5d89db9d82273f1d18970dfc"
    },
    "user": {
        "$oid": "5d24bbd89f09024590db9efd"
    },
    "isDeleted": true
}

当我使用以下内容对发布ID进行查找时,会得到完美的结果:

Subscriber.aggregate([
      {
        $match: {
          $and: [
            { 'publication': mongoose.Types.ObjectId(req.params.id) },
          ],
          "$expr": { "$eq": [{ "$year": "$subStartDate" }, new Date().getFullYear()] }
        }
      },
      {
        /* group by year and month of the subscription event */
        $group: {
          _id: { year: { $year: "$subStartDate" }, month: { $month: "$subStartDate" }, subType: "$subType" },
          count: { $sum: 1 }
        },
      },
      {
        /* sort descending (latest subscriptions first) */
        $sort: {
          '_id.year': -1,
          '_id.month': -1
        }
      },
      {
        $limit: 100,
      },
    ])

但是,当我想从阅读器集合(订阅者模型)接收所有年份的数据时,我没有从所有尝试中得到的预期结果(如果有)(我正在发布最佳尝试结果如下:

Publication.aggregate([
      {
        $match:
        {
          user: mongoose.Types.ObjectId(id),
          isDeleted: false
        }
      },
      {
        $project: {
          _id: 1,
        }
      },
      {
        $lookup: {
          from: "readercollections",
          let: { "id": "$_id" },
          pipeline: [
            {
              $match:
              {
                $expr: {
                  $and: [
                    { $eq: ["$publication", "$$id"] },
                    { "$eq": [{ "$year": "$subStartDate" }, new Date().getFullYear()] }
                  ],

                }
              }
            },
            { $project: { subStartDate: 1, subType: 1 } }
          ],
          as: "founditem"
        }
      },
      // {
      //   /* group by year and month of the subscription event */
      //   $group: {
      //     _id: { year: { $year: "$founditem.subStartDate" }, month: { $month: "$foundtitem.subStartDate" }, subType: "$founditem.subType" },
      //     count: { $sum: 1 }
      //   },
      // },
      // {
      //   /* sort descending (latest subscriptions first) */
      //   $sort: {
      //     '_id.year': -1,
      //     '_id.month': -1
      //   }
      // }, 
    ], function (err, result) {
      if (err) {
        console.log(err);
      } else {
        res.json(result);
      }
    })

哪些返回没有$ group的所需数据(注释掉了),但是我需要$ group起作用,否则我将不得不基于月份和子类型来映射动态数组,这是完全无效的。

当我进行诊断时,似乎是$ group这个问题,但是我无法看到如何解决它,因为它在单个$ year / $ month组中起作用。所以我尝试了以下方法:

 {
        /* group by year and month of the subscription event */
        $group: {
          _id: { year: { $year: "$subStartDate" }, month: { $month: "$subStartDate" }, subType: "$founditem.subType" },
          count: { $sum: 1 }
        },
      },

它返回了$ founditem.subType罚款,但是任何计数或尝试获取$ founditem.subStartDate的$ year或$ month都导致BSON错误。

有效(并已完美插入折线图)的阅读器集合调用中的单个发布ID查找的输出为:

[
    {
        "_id": {
            "year": 2019,
            "month": 11,
            "subType": "digital"
        },
        "count": 1
    },
    {
        "_id": {
            "year": 2019,
            "month": 11,
            "subType": "print"
        },
        "count": 3
    }
]

这是我想要的所有出版物的输出,而不仅仅是在阅读器集合中对出版物ID的单次查找。

谢谢您的协助,如果您需要更多详细信息,请告诉我!

0 个答案:

没有答案