Mongo $lookup 在另一个 $group 之后不起作用

问题描述 投票:0回答:1

我有一个像这样的聚合查询来对有帖子的类别进行分组

    const categoriesWithPosts = await Models.Post.aggregate([
      {
        $match: { active: true },
      },
      {
        $group: {
          _id: "$category",
        },
      },
      {
        $lookup: {
          from: 'categories',
          localField: '_id',
          foreignField: '_id',
          as: 'category',
        },
      },
      {
        $unwind: '$category',
      },
      {
        $group: {
          _id: '$category.parent_id',
          subCategories: {
            $push: '$category',
          },
        },
      },
      // {
      //   $lookup: {
      //     from: 'categories',
      //     localField: '_id',
      //     foreignField: '_id',
      //     as: 'parentCategory',
      //   },
      // },
      // {
      //   $unwind: '$parentCategory',
      // },

    ]);

现在它工作正常,直到未注释的行将我的类别分组为这样

[
{
                "_id": "655b01fca9233b324cdc5051",
                "subCategories": [
                    {
                        "_id": "655b05c5198d38433e066970",
                        "parent_id": "655b01fca9233b324cdc5051",
                        "title": "Crafts and Hobbies",
                        "image": "https://images.ctfassets.net/nvayryb6yxxr/7oviwG8t6myzR4DkCSP9gD/037c75622dd6aa8000e633726f48598a/Crafts_and_Hobbies.jpg",
                        "__v": 0
                    },
                    {
                        "_id": "655b02e6c3246efb0f5174f5",
                        "parent_id": "655b01fca9233b324cdc5051",
                        "title": "Home Decor",
                        "image": "https://images.ctfassets.net/nvayryb6yxxr/5QJRgVkbP2h7WTyfIfRJvN/2d0cf22fb38661ed838769472f6d236f/Home_Decor_Category.jpg",
                        "__v": 0
                    }
                ]
            },
]

但是当我取消注释该行以查找父类别时,它现在返回一个空数组,我不知道为什么要这样做,我的 mongo 有我将显示的 id,但它没有获取它

{
  "_id": {
    "$oid": "655b01fca9233b324cdc5051"
  },
  "parent_id": "",
  "title": "Home",
  "image": "https://images.ctfassets.net/nvayryb6yxxr/49RTYuPnpuYROSJ7S57Slw/8ff9173435a5e2114a43cad31afa5d45/Home_Category.jpg",
  "__v": 0
}

也会分享品类模型

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

const categorySchema = new Schema({
    parent_id: { type: String, default: "" },
    title: { type: String, required: true },
    image: { type: String },
});

const Category = mongoose.model("category", categorySchema);

module.exports = Category;
node.js mongodb mongodb-query aggregation-framework
1个回答
0
投票

我认为问题在于parent_id是一个字符串,而

_id
categories
ObjectId
$oid
)。

在与parentId进行比较时,您应该将_id解析为字符串。您可以执行以下操作:

   {
     $lookup: {
       from: 'categories',
       let : {"parentId" : "$_id"},
       pipeline : [
          {$match : 
              {$expr: 
                  {$eq : [
                      "$$parentId",
                      {"$toString" : "$_id"}
                         ]
                  }
              }
          }
       ],
       as: 'parentCategory',
     },
   },

PS:您可以采取相反的方式,将您的parentId解析为ObjectId,但如果它与ObjectId的格式不对应,它可能会返回错误。

© www.soinside.com 2019 - 2024. All rights reserved.