Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
post schema:
    const BlogSchema = new mongoose.Schema(
    {
        
        title: {
            type: String
          },
          content: {
            type: String
          },
          author: {
            type: mongoose.Schema.Types.ObjectId,
            ref: Tags
          },
          content_type: {
            type: mongoose.Schema.Types.ObjectId,
            ref: Tags
          },
          valid_until: {
            type: Date,
          },
          timestamp: {
            type: Date,
            default: Date.now
          },
    likes: { type: [String], default: [] },

    },
);

user schema:
    const mongoose = require('mongoose')

const UserProfileSchema = mongoose.Schema({
    userid: {
        type: String
    },
    firstname: {
        type: String,
        default: ''
    },
    tags: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Tag'
    }]
})

tag schema:
    const TagSchema = new Schema({
    name: {
        type: String
    },
    type: {
        type: Object,
    },
    color: {
        type: String
    },
    timestamp: {
        type: Date,
        default: Date.now
    },
});

this code is in post controller it will aggregate posts
const userid = req.params.id
    const blogs = await Blog.aggregate([
                {
                    $lookup: {
                        from: "tags",
                        localField: "author",
                        foreignField: "_id",
                        as: "blog_tags"
                    }
                }])

What can i do to make it only get posts that are in user tags

What I have tried:

const userid = req.params.id
const blogs = await Blog.aggregate([
                {
                    $lookup: {
                        from: "tags",
                        localField: "author",
                        foreignField: "_id",
                        as: "blog_tags"
                    }
                }])
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900