I have post schema like this
const postSchema = new mongoose.Schema({
title: { type: String, minlength: 2 },
description: { type: String },
categoryId: [{ type: mongoose.Schema.ObjectId, ref: "category" }],
});
I have to fetch random documents from the posts and populate all categories in it. What I have achieved so far is this
await Post.aggregate([
{
$lookup: {
from: "categories",
localField: "categoryId",
foreignField: "_id",
as: "categoryId",
},
},
]).sample(5)
Need to do
Suppose i have an id of a category in a variable catId, what I need to do is to filter out those posts which contains this id in their array of categoryId before getting those random posts. By filtering out I mean i need random posts which has catId in their array.