Answers for "mongodb aggregation part 3"

0

mongodb aggregation part 3

} else {
      posts = await Post.aggregate([
        {
          $lookup: {
            from: "users",
            let: { userId: "$userId" },
            pipeline: [
              {
                $match: {
                  $expr: { $eq: ["$_id", "$$userId"] },
                },
              },
              { $project: { fullName: 1, userName: 1, email: 1, image: 1, privateAccount: 1 } },
            ],
            as: "users",
          },
        },
        {
          $addFields: {
            userId: {
              $arrayElemAt: ["$users", 0],
            },
          },
        },
        {
          $match: {
            $and: [
              { "userId._id": { $nin: blockedUserIds } },
              {
                "userId._id": { $nin: followingListIds },
              },
              { "userId._id": { $ne: mongoose.Types.ObjectId(userId) } },
            ],
          },
        },
        //below condition is to filter out the blocked users data
        {
          $match: {
            $or: [
              { "userId.privateAccount": { $exists: false } },
              {
                $and: [
                  { "userId.privateAccount": { $exists: true } },
                  { "userId.privateAccount": { $ne: "true" } },
                ]
              }
            ]
          }
        },
        {
          $lookup: {
            from: "ingredients",
            localField: "ingredients.id",
            foreignField: "_id",
            as: "output"
          }
        },
        {
          $addFields: {
            "ingredients.name": "$output.name"
          }
        },
        { $project: { output: 0, postLikes: 0, userLiked: 0, ingredient: 0, dayssince: 0, users: 0, comments: 0, image: 0, video: 0 } },
        { $sort: { createdAt: -1 } },
        {
          $facet: {
            stage1: [{ $group: { _id: null, count: { $sum: 1 } } }],
            stage2: [
              {
                $skip: (allPostsPage - 1) * limit,
              },
              { $limit: limit * 1 },
            ],
          },
        },
        {
          $unwind: {
            path: "$stage1",
          },
        },
        {
          $project: {
            count: "$stage1.count",
            data: "$stage2",
          },
        },
      ]);
      allPostsPage = allPostsPage + 1;
    }
  }

  postsCount = posts[0] ? posts[0].count : 0;
  posts = posts[0] ? posts[0].data : [];

  if (initialPosts.length > 0) {
    posts = initialPosts.concat(posts);
  }

  const postIds = posts.map((post) => post._id)

  let postLikesCount = 0
  let postCommentsCount = 0
  let isLiked = 0

  const postLikes = await Like.find({ postId: { $in: postIds }, like: true })
  const postComments = await Comment.find({ postId: { $in: postIds } })

  const updatedPosts = posts.map((post) => {
    postLikes.map((singleLike) => {
      if (post._id.toString() === singleLike.postId.toString()) {
        postLikesCount = postLikesCount + 1
      }
      if (userId.toString() === post.userId.toString()) {
        isLiked = isLiked + 1
      }
    })

    postComments.map((singleComment) => {
      if (post._id.toString() === singleComment.postId.toString()) {
        postCommentsCount = postCommentsCount + 1
      }
    })

    return { ...post, postLikesCount, postCommentsCount }
  })

  return res.status(200).send({
    posts: updatedPosts,
    totalPages: fakeTotalPagesFlag ? 2 : Math.ceil(postsCount / limit),
    currentAllPostsPage: parseInt(allPostsPage),
    currentFollowingPostsPage: parseInt(followingPostsPage),
    noMoreFollowingPosts: noMoreFollowingPosts,
  });
};
Posted by: Guest on March-08-2022

Code answers related to "mongodb aggregation part 3"

Browse Popular Code Answers by Language