【问题标题】:fetch 3 schema value using mongoose node JS使用猫鼬节点 JS 获取 3 个模式值
【发布时间】:2020-04-25 14:39:27
【问题描述】:

我有 3 个集合架构 CategorySchema、SubCategorySchema、ProductSchema,如下所示。

var CategorySchema = new mongoose.Schema({  
  catgory_name: {
    type: String,
    required: [true, "Catgory name is required"]
  },  
  modified_date: {
    type: Date
  }
});

module.exports = mongoose.model("Category", CategorySchema);
var SubCategorySchema = new Schema({  
  subcatgory_name: {
    type: String,
    required: [true, "subcategory name is required"]
  },  
  category_id: {
    type: Schema.Types.ObjectId,
    ref: "Category",
    required: [true, "category id is required"]
  },  
  modified_date: {
    type: Date
  },  
  is_active: {
    type: Boolean,
    default: 1
  }
});

module.exports = mongoose.model("SubCategories", SubCategorySchema);

const ProductSchema = new Schema({  
  product_name: {
    type: String,
    required: [true, "Product name is required"]
  },  
  product_image: {
    type: String,
    required: [true, "Product image is required"]
  },  
  category_id: {
    type: Schema.Types.ObjectId,
    ref: "Category",
    required: [true, "category is required"]
  },  
  subcategory_id: {
    type: Schema.Types.ObjectId,
    ref: "Subcategory",
    required: [true, "Subcategory is required"]
  },  
  modified_date: {
    type: Date
  },  
  is_active: {
    type: Boolean,
    default: 1
  }
});

module.exports = mongoose.model("Products", ProductSchema);

在这里,我想获取所有有效产品 (is_active = 1) 以及相应的类别和有效子类别 (is_active = 1)。无需检查类别的 is_active 条件,但需要检查子类别和产品的活动条件

我在节点 JS 控制器中尝试了以下代码

router.get("/list", (req, res, next) => {
  products
    .find({ is_active: true })
    .populate("category_id")
    .populate("subcategory_id", null, SubCategory, {
      match: { is_active: true }
    })
    //.where("subcategory_id", !null)
    .then(products => res.json({ status: 200, data: products }))
    .catch(err => res.json(err));
});

但即使子类别处于非活动状态,它也会返回产品数据

【问题讨论】:

    标签: node.js mongoose


    【解决方案1】:

    您可以使用 mongodb 聚合框架进行查询,但仍使用 mongoose。

    router.get("/list", (req, res, next) => {
      products
        .aggregate([
          {
            $match: {
              is_active: true
            }
          },
          {
            $lookup: {
              from: "subcategories",
              localField: "subcategory_id",
              foreignField: "_id",
              as: "subcategories"
            }
          },
          {
            $unwind: "$subcategories"
          },
          {
            $match: {
              "subcategories.is_active": true
            }
          },
          {
            $lookup: {
              from: "categories",
              localField: "category_id",
              foreignField: "_id",
              as: "category"
            }
          },
          {
            $addFields: {
              category: {
                $arrayElemAt: ["$category", 0]
              }
            }
          }
        ])
        .then(products => res.json({ status: 200, data: products }))
        .catch(err => res.status(500).json(err));
    });
    

    Playground

    让我们有这些示例文档:

    db={
      "products": [
        {
          "modified_date": "2020-01-08T09:06:51.544Z",
          "is_active": true,
          "_id": "5e159ca1bd95457404b22bc3",
          "product_name": "Product1 Name",
          "product_image": "Product1 Image",
          "category_id": "5e159b77a746036404b5f0ae",
          "subcategory_id": "5e159befbd95457404b22bc2"
        },
        {
          "modified_date": "2020-01-08T09:06:51.544Z",
          "is_active": false,
          "_id": "5e159cb8bd95457404b22bc4",
          "product_name": "Product2 Name",
          "product_image": "Product2 Image",
          "category_id": "5e159b77a746036404b5f0ae",
          "subcategory_id": "5e159befbd95457404b22bc2"
        },
        {
          "modified_date": "2020-01-08T09:06:51.544Z",
          "is_active": true,
          "_id": "5e159d3abd95457404b22bc6",
          "product_name": "Product3 Name",
          "product_image": "Product3 Image",
          "category_id": "5e159b77a746036404b5f0ae",
          "subcategory_id": "5e159ce0bd95457404b22bc5"
        }
      ],
      "categories": [
        {
          "modified_date": "2020-01-08T09:04:18.003Z",
          "_id": "5e159b77a746036404b5f0ae",
          "catgory_name": "Main Category 1"
        }
      ],
      "subcategories": [
        {
          "modified_date": "2020-01-08T09:06:51.544Z",
          "is_active": true,
          "_id": "5e159befbd95457404b22bc2",
          "subcatgory_name": "Sub Category 1",
          "category_id": "5e159b77a746036404b5f0ae"
        },
        {
          "modified_date": "2020-01-08T09:06:51.544Z",
          "is_active": false,
          "_id": "5e159ce0bd95457404b22bc5",
          "subcatgory_name": "Sub Category 2",
          "category_id": "5e159b77a746036404b5f0ae"
        }
      ]
    }
    

    结果将是:

    [
      {
        "_id": "5e159ca1bd95457404b22bc3",
        "category": {
          "_id": "5e159b77a746036404b5f0ae",
          "catgory_name": "Main Category 1",
          "modified_date": "2020-01-08T09:04:18.003Z"
        },
        "category_id": "5e159b77a746036404b5f0ae",
        "is_active": true,
        "modified_date": "2020-01-08T09:06:51.544Z",
        "product_image": "Product1 Image",
        "product_name": "Product1 Name",
        "subcategories": {
          "_id": "5e159befbd95457404b22bc2",
          "category_id": "5e159b77a746036404b5f0ae",
          "is_active": true,
          "modified_date": "2020-01-08T09:06:51.544Z",
          "subcatgory_name": "Sub Category 1"
        },
        "subcategory_id": "5e159befbd95457404b22bc2"
      }
    ]
    

    如您所见,即使产品 3 处于活动状态,它也未被检索到,因为它的子类别 5e159ce0bd95457404b22bc5 未处于活动状态。

    【讨论】:

    • 是的,知道了,谢谢@suleymanSah。但我有一个问题,为什么我不应该使用 populate 而不是 $lookup
    • 另外,如果我想从输出 json 的类别和子类别对象中删除修改日期,我可以在查询本身中实现它吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 2015-06-20
    • 2020-06-03
    • 2022-01-26
    • 2020-12-05
    • 2015-11-19
    • 2018-12-29
    相关资源
    最近更新 更多