无法使用聚合填充和构建文档

Unable to populate and structure document with aggregate

所以我有这个模式,它有数据库中其他集合的外键。该文档有大约 60k posts,每个 post 可以有多个类别,大约有 200 个类别。所以我尝试根据类别的外键获取和构建数据并填充类别详细信息和计数。

主架构和类别架构如下所示:

const postSchema = new mongoose.Schema( {
    post: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'PostDetails'
    },
    categories: [ {
        category: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Category'
        },
        subCategories: [ {
            subCategory: {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'Subcategory'
            }
        } ]
    } ]
} );

const categorySchema = new mongoose.Schema( {
    category: {
        type: String,
    },
    categorySlug: {
        type: String,
    }
} );

我计数成功了,但是返回的数据不是我所期望的。返回的数据显示类别的 ID 和计数,但没有名称和 slug。它是这样的:

[
   {
       "_id": [
                 "617acfd232c766589c23a90c"
       ],
       "count": 876,
       "category": []
   }
]

我通过以下查询获得了以上输出:

const aggregateStages = [ 
     {
          $group: {
                _id: '$categories.category',
                count: { $sum: 1 }
          }
     },
     {
          $lookup: {
                from: "Category",
                localField: "categories.category",
                foreignField: "_id",
                as: "category"
          }
     }
];

const categories = await Post.aggregate( aggregateStages ).exec();

我希望得到如下数据:

[
   {
       "_id": "617acfd232c766589c23a90c",
       "count": 876,
       "category": 'SomeCategory',
       "categorySlug": 'some-category' 
   }
]

我哪里出错了,我该如何解决?

根据 MA​​TT OESTREICH 的要求,来自数据库的示例数据

POST DATA

{
    "_id": "617adad39054bae2c983c34f",
    "post": "617ad1c80597c78ed4cc151e",
    "author": "617acc689b309fdbbbdfdfe0",
    "categories": [{
        "category": "617acfd232c766589c23a8d1",
        "subCategories":[]
    }]
}

CATEGORY DATA

{
    "_id": "617acfd232c766589c23a8d1",
    "category": "Lorem Ipsum",
    "categorySlug": "lorem-ipsum"
}

好的,看来您可以使用 $size 运算符解决此问题。 $size 运算符将为您提供数组中元素的长度(或计数)。

Live demo here

数据库

db={
  "post": [
    {
      "_id": ObjectId("617adad39054bae2c983c34f"),
      "post": ObjectId("617ad1c80597c78ed4cc151e"),
      "author": ObjectId("617acc689b309fdbbbdfdfe0"),
      "categories": [
        {
          "category": ObjectId("617acfd232c766589c23a8d1"),
          "subCategories": []
        }
      ]
    }
  ],
  "categories": [
    {
      "_id": ObjectId("617acfd232c766589c23a8d1"),
      "category": "Lorem Ipsum",
      "categorySlug": "lorem-ipsum"
    }
  ]
}

查询

db.post.aggregate([
  {
    "$lookup": {
      "from": "categories",
      "localField": "categories.category",
      "foreignField": "_id",
      "as": "found_categories"
    }
  },
  {
    "$project": {
      _id: "$_id",
      count: {
        "$size": "$found_categories"
      },
      "category": {
        "$first": "$found_categories.category"
      },
      "categorySlug": {
        "$first": "$found_categories.categorySlug"
      }
    }
  }
])

结果

[
  {
    "_id": ObjectId("617adad39054bae2c983c34f"),
    "category": "Lorem Ipsum",
    "categorySlug": "lorem-ipsum",
    "count": 1
  }
]

不过,如果找到多个类别,我认为这不会为您提供所需的内容。如果它不起作用,请告诉我,我会尽力帮助修复它。