const mongoose = require("mongoose");
const { ObjectId } = mongoose.Schema;
const productSchema = new mongoose.Schema(
{
title: {
type: String,
trim: true,
required: true,
maxlength: 32,
text: true,
},
category: {
type: ObjectId,
ref: "Category",
},
)
I am tring to filter products based on category name, but cateogry itself is a different collection. How can I apply filter conditions correctly as current one is not working.
const products = await Product.find({ title:title, "category.name": categ})
const categorySchema = new mongoose.Schema(
{
name: {
type: String,
trim: true,
required: "Name is required",
minlength: [2, "Too short"],
maxlength: [32, "Too long"],
},
{ timestamps: true }
);
2
Answers
To get access to
category.name
you need to use the populate method. However, because you are using referenced docs, which is a perfectly good design for your schemas, it unfortunately means that you can’t filter parent documents based on a condition of a referenced child document as per the docsThankfully you can use
aggregate()
to get what you need.The mongoose Model.aggregate method allows you to pass mongodb aggregation pipeline stages. This can be used in your case to populate all of your
Product
documents with their referencedCategory
documents. Then once they are embedded you can then match oncategory.name
like so:See HERE for a working example where a user searches for the
Product
title:'Hat'
from theCategory
ofname:'Mens'
.I think you can use populate to get values from your query. Just update the query as per below.
or else you can use aggregation pipeline solve but i think populate will be more performant way to solve question.You can also define virtuals for populate that makes easy your task.
refer to similer Question