英文:
search for category in reference document in nodejs
问题
类别模式
const CategorySchema = mongoose.Schema(
{
catName: { type: String, required: true }
},
{ timestamps: true }
);
产品模式
const ProductSchema = new mongoose.Schema(
{
brand: { type: String, required: true },
title: { type: String, required: true },
categories: { type: mongoose.Schema.Types.ObjectId, ref: "Category" },
},
{ timestamps: true }
);
这是我用来搜索类别名称的代码,同时知道我的产品模型中存储的是类别模型的引用 ID。
const qCategory = req.query.category;
else if (qCategory) {
products = await Product.find({
categories: {
catName: qCategory
}
}).populate("categories");
}
我已经尽力了,请帮助我解决问题。
我需要按类别搜索。
英文:
category schema
const CategorySchema = mongoose.Schema(
{
catName: { type: String, required: true }
},
{ timestamps: true }
);
Product Schema
const ProductSchema = new mongoose.Schema(
{
brand: { type: String, required: true },
title: { type: String, required: true },
categories: { type: mongoose.Schema.Types.ObjectId, ref: "Category" },
},
{ timestamps: true }
);
**
This is my code to search for category name while knowing that the data stored in my product model is a reference Id from category model**
const qCategory = req.query.category;
else if (qCategory) {
products = await Product.find({
categories: {
catName: qCategory
}
}).populate("categories");
}
I have tried whatever I know, Please help me out.
I need to search by categories
答案1
得分: 1
你需要首先获取类别的ID,然后使用这个ID来查询产品:
const qCategory = req.query.category;
const category = await Category.findOne({ catName: qCategory });
if (category) {
products = await Product.find({ categories: category._id }).populate("categories");
}
另外,我认为你想把名称改成单数的 "category",因为目前它只允许存储一个。
如果你想存储多个,需要进行以下更改:
categories: [{ type: mongoose.Schema.Types.ObjectId, ref: "Category" }]
英文:
You need to get the id of the category first, then use this id to query the products:
const qCategory = req.query.category;
const category = await Category.findOne({ catName: qCategory });
if (category) {
products = await Product.find({ categories: category._id }).populate("categories");
}
Also I think you wanna change the name to singular category, because currently it allows to store just one.
If you want to store multiple you need to change for this:
categories: [{ type: mongoose.Schema.Types.ObjectId, ref: "Category" }],
答案2
得分: 1
你可以尝试这样做:
router.get("/search/:key", async (req, res) => {
try {
const category = await Category.findOne({ catName: req.params.key });
let products = await Product.find({
$or: [
{ title: { $regex: req.params.key, $options: "i" } },
{ brand: { $regex: req.params.key, $options: "i" } },
{ categories: category?._id }
]
});
} catch {
}
});
英文:
You can try this:
router.get("/search/:key", async (req, res) => {
try {
const category = await Category.findOne({ catName: req.params.key });
let products = await Product.find({
$or: [
{ title: { $regex: req.params.key, $options: "i" } },
{ brand: { $regex: req.params.key, $options: "i" } },
{ categories: category?._id }
]
});
} catch {
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论