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"
}
}])