i have a query in mongodb, i want to get from collection by specific field the field is “private.group” and its boolean.
i want the $match will be dynamic to pass is as varible (pass is true or false) and then make this query work as private group or public group.
how can i make this if tenary work in $match?
i can pass parameter to the function but its not work with if tenary
i mean this line of group.private
export const getAllPrivateGroupsData = async (
userId: string,
pagination: Pagination
) => {
const pipeline = [
{
$match: {
userId: userId,
groupId: { $exists: true },
},
},
{
$lookup: {
from: "groups",
localField: "groupId",
foreignField: "_id",
as: "group",
},
},
{
$unwind: "$group",
},
{
$match: {
"group.private": true, // Filter only public groups
},
},
{
$lookup: {
from: "events",
localField: "group.eventsIds",
foreignField: "_id",
as: "events",
},
},
{
$unwind: {
path: "$events",
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: "event_images",
localField: "events.images",
foreignField: "_id",
as: "eventImages",
},
},
{
$lookup: {
from: "event_comments",
localField: "events.comments",
foreignField: "_id",
as: "eventComments",
},
},
{
$lookup: {
from: "event_members",
localField: "events._id",
foreignField: "eventId",
as: "eventMembers",
},
},
{
$group: {
_id: "$group._id",
name: { $first: "$group.name" },
imageCover: { $first: "$group.imageCover" },
private: { $first: "$group.private" },
createdAt: { $first: "$group.createdAt" },
admin: { $first: "$group.admin" },
status: { $first: "$status" },
neighborhood: { $first: "$group.neighborhood" },
institution: { $first: "$group.institution" },
description: { $first: "$group.description" },
events: {
$push: {
$mergeObjects: [
{
userType: {
$cond: {
if: {
$in: [
userId,
"$eventMembers.userId",
],
},
then: "member",
else: "visitor",
},
},
},
"$events",
{ images: "$eventImages" },
{ comments: "$eventComments" },
{ eventMembers: "$eventMembers" },
],
},
},
},
},
{ $sort: { _id: -1 } },
]
return await DB.withPagination(
DB.db?.collection("group_members").aggregate(pipeline),
pagination
)?.toArray()
}