Answers for "mongodb aggregate part 2"

0

mongodb aggregate part 2

if (count < 25) {
      // fakeTotalPagesFlag = true;
      if (count < 3) {
        fetchOthersPosts = "true";
        initialPosts = posts[0] ? posts[0].data : [];
      }
    }

    if (filteredPostsCount < 25) {
      noMoreFollowingPosts = true;
    }
    if (filteredPostsCount == 0) {
      fetchOthersPosts = "true";
    }
    followingPostsPage = followingPostsPage + 1;
  }

  if (fetchOthersPosts === "true") {
    noMoreFollowingPosts = true;
    if (noFollowers) {
      blockedUserIds.push(mongoose.Types.ObjectId(userId));
      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],
            },
            dayssince: {
              $trunc: {
                $divide: [
                  { $subtract: [new Date(), "$createdAt"] },
                  1000 * 60 * 60 * 24,
                ],
              },
            },
          },
        },
        {
          $match: {
            $or: [
              { "userId._id": { $nin: blockedUserIds } },

              {
                $and: [
                  { "userId._id": mongoose.Types.ObjectId(userId) },
                  { dayssince: { $lt: 2 } },
                ],
              },
            ],
          },
        },
        //below condition is to filter out the blocked users data
        {
          $match: {
            $or: [
              {
                $and: [
                  { "userId._id": mongoose.Types.ObjectId(userId) },
                  { dayssince: { $lt: 2 } },
                ],
              },
              {
                $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;
Posted by: Guest on March-08-2022

Code answers related to "mongodb aggregate part 2"

Browse Popular Code Answers by Language