在Node.js中搜索参考文档中的类别。

huangapple go评论59阅读模式
英文:

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 {

    }
});

huangapple
  • 本文由 发表于 2023年6月1日 05:06:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定