node.js - 使用 mongoose Node JS 获取 3 个模式值

标签 node.js mongoose

我有 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 条件,但需要检查子类别和产品的 active 条件

我在 Node JS Controller 中尝试使用以下代码

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));
});

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

最佳答案

您可以使用 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 未处于事件状态。

关于node.js - 使用 mongoose Node JS 获取 3 个模式值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59639443/

相关文章:

node.js - 编译 Rails 3.1 Assets 的 ExecJS 问题

node.js - 异步/等待发布请求-nodejs

javascript - 未知的顶级运营商 : $orderby

javascript - 是否可以更新 Mongoose 文档中的对象?

javascript - 无法读取未定义的外部 page.evaluate() 属性 'map'

mysql - Nodejs和mysql重复问题

javascript - 使用 ORM Sequelize/node.js 操作数据

javascript - 当key有空格和点时如何在mongodb中查询

node.js - mongodb使用更新来避免竞争条件,当字段为空数组时更新错误

javascript - 如何从 Mongoose 中的多个数组更新数组内的值?