Daniel Greatman Walter
Daniel Greatman Walter

Reputation: 157

How can i reference a field in another mongoose model so i can retrieve multiple documents from different collections

This is my category schema

const subcategories = require('./Subcategory')

const CategorySchema = mongoose.Schema({
    name: {
        type: String, required: true
    },
    icon: {
        type: String, required: false
    },
    status: {
        type: Number, default: 0
    },
    created : {
        type : Date,
        default: Date.now()
    },
    subcategories: {
        type: mongoose.Schema.Types.ObjectId,
        ref: subcategories
    }
});

this is my subcategory schema

const SubcategorySchema = mongoose.Schema({
    category_id: {
        type: String, required: true
    },
    name: {
        type: String, required: true
    },
    status: {
        type: Number, default: 0
    },
    icon: {
        type: String, required: false
    },
    created : {
        type : Date,
        default: Date.now()
    }
});

Every subcategory has category_id which serves as a relation between a category and multiple subcategories. How can I set the ref in my category model in such a way that when I retrieve a category, all the subcategories with the same category_id as my category will be retrieved?

In my category model/schema, I tried

    subcategories: {
        type: mongoose.Schema.Types.ObjectId,
        ref: subcategories
    }

and it did not work. I also tried

subcategories: {
   type: mongoose.Schema.Types.ObjectId,
   ref: subcategories.category_id
}

and it didn't work i was getting only documents from my category collection and I was it getting the corresponding subcategories. The category and subcategory collection has a one-to-many relationship.

This is the line of code to retrieve the data

const result = await CategoryModel.find().populate('subcategories');

this is the result i was getting

    {
      status: 1,
      created: 2020-06-10T12:48:37.375Z,
      _id: 5ee0d6d8d08a131d68889c66,
      name: 'Fashion',
      __v: 0
    }

Upvotes: 2

Views: 5409

Answers (1)

warl0ck
warl0ck

Reputation: 3464

You will need to reference mongoose model in your schema, instead of schema.

Like:

Subcategory schema

const SubcategorySchema = mongoose.Schema({
    category_id: {
        type: String, required: true
    },
    name: {
        type: String, required: true
    },
    status: {
        type: Number, default: 0
    },
    icon: {
        type: String, required: false
    },
    created : {
        type : Date,
        default: Date.now()
    }
});

const SubCategoryModel = mongoose.model('SubCategory', SubcategorySchema);

CategorySchema

const subcategories = require('./Subcategory')

const CategorySchema = mongoose.Schema({
    name: {
        type: String, required: true
    },
    icon: {
        type: String, required: false
    },
    status: {
        type: Number, default: 0
    },
    created : {
        type : Date,
        default: Date.now()
    },
    subcategories: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "SubCategory" // this name should be same as the model name specified while declaring model
    }
});

Upvotes: 1

Related Questions