0

I have two models, a posts model and a category model where I have an array that stores posts by objectId

Category Model

const mongoose = require('mongoose');

const CategorySchema = new mongoose.Schema(
    {
        name: {
            type: String,
            required: true,
        },
        color: {
            type: String,
            required: true,
        },
        posts: [{
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Post',
            required: false,
        }],
        createdAt: {
            type: Date,
            default: Date.now,
        }
    },
    { timestamps: true }
  );
  
module.exports = mongoose.model("Category", CategorySchema);

Post model

const mongoose = require('mongoose');

const PostSchema = new mongoose.Schema(
    {
        title: {
            type: String,
            required: true,
        },
        img: {
            type: String,
            required: true,
        },
        category: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Category",
        },
        desc: {
            type: String,
            required: false,
        },
        createdAt: {
            type: Date,
            default: Date.now,
        }
    },
    { timestamps: true }
  );
  
module.exports = mongoose.model("Post", PostSchema);

So I created a get by id category controller

const Category = require('../../models/Category');

class FindCategory {
    async find(req, res) {
        const { id } = req.params;

        try {
            const category = await Category.findById(id);

            return res.status(200).json(category);
        } catch (err) {
            return res.status(500).json(err);
        }
    }
} 

module.exports = new FindCategory();

The problem is that when I make this request in my postman, it returns me for example

{
    "_id": "63ac925d872445065a588f61",
    "name": "Games",
    "color": "#ff914d",
    "posts": [
        "63ac9cbccec4d9f35c4f4d1f"
    ],
    "createdAt": "2022-12-28T19:00:45.847Z",
    "updatedAt": "2022-12-28T19:49:47.119Z",
    "__v": 0
}

But I would like to render the information of each post inside the "posts" array, something like that for example

{
    "_id": "63ac925d872445065a588f61",
    "name": "Games",
    "color": "#ff914d",
    "posts": [
        "name": "red dead",
        "img": "example.png", 
        "category": "games",
        "desc": "test,
    ],
    "createdAt": "2022-12-28T19:00:45.847Z",
    "updatedAt": "2022-12-28T19:49:47.119Z",
    "__v": 0
}

1 Answer 1

1

You should use the populate function provided by mongoose: https://mongoosejs.com/docs/populate.html#setting-populated-fields

Category.
  findById(...).
  populate('posts')
Sign up to request clarification or add additional context in comments.

1 Comment

A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so others will have some idea what it is and why it’s there, then quote the most relevant part in case the linked page is unavailable. If you edit this answer to add more once it is deleted, please flag it for moderator attention so that it can be un-deleted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.