0

I made that aggregate query below to handle a social network main feed that will be able to show the posts and then answer then. Is there a way to simplify that query or the right choice for that case is to lookup, unwind and project?

`[
      {
        $match: {
          _id: mongoose.Types.ObjectId(myId),
        },
      },
      {
        $addFields: {
          following: {
            $concatArrays: ["$following", [mongoose.Types.ObjectId(myId)]],
          },
        },
      },
      {
        $lookup: {
          from: "users",
          localField: "following",
          foreignField: "_id",
          as: "following",
        },
      },
      {
        $unwind: {
          path: "$following",
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $project: {
          _id: "$following._id",
          name: "$following.name",
          surname: "$following.surname",
          profilePicPath: "$following.profilePicPath",
        },
      },
      {
        $lookup: {
          from: "posts",
          localField: "_id",
          foreignField: "parentId",
          as: "posts",
        },
      },
      {
        $unwind: {
          path: "$posts",
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $project: {
          name: true,
          surname: true,
          profilePicPath: true,
          _id: "$posts._id",
          parentId: "$posts.parentId",
          content: "$posts.content",
          date: "$posts.date",
        },
      },
      {
        $lookup: {
          from: "answerposts",
          localField: "_id",
          foreignField: "parentId",
          as: "answerPosts",
        },
      },
      {
        $unwind: {
          path: "$answerPosts",
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $project: {
          name: true,
          surname: true,
          profilePicPath: true,
          _id: true,
          parentId: true,
          content: true,
          date: true,
          answerPosts_id: "$answerPosts._id",
          answerPostsOwnerId: "$answerPosts.ownerId",
          answerPostsContent: "$answerPosts.content",
          answerPostsDate: "$answerPosts.date",
        },
      },
      {
        $lookup: {
          from: "users",
          localField: "answerPostsOwnerId",
          foreignField: "_id",
          as: "answerPostsOwner",
        },
      },
      {
        $unwind: {
          path: "$answerPostsOwner",
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $project: {
          name: true,
          surname: true,
          profilePicPath: true,
          _id: true,
          parentId: true,
          content: true,
          date: true,
          answerPosts_id: true,
          answerPostsContent: true,
          answerPostsDate: true,
          answerPostsOwner_name: "$answerPostsOwner.name",
          answerPostsOwner_surname: "$answerPostsOwner.surname",
          answerPostsOwner_profilePicPath: "$answerPostsOwner.profilePicPath",
        },
      },
      {
        $group: {
          _id: "$_id",
          parentId: {
            $first: "$parentId",
          },
          name: {
            $first: "$name",
          },
          surname: {
            $first: "$surname",
          },
          profilePicPath: {
            $first: "$profilePicPath",
          },
          content: {
            $first: "$content",
          },
          date: {
            $first: "$date",
          },
          answerPosts: {
            $push: {
              _id: "$answerPosts_id",
              name: "$answerPostsOwner_name",
              surname: "$answerPostsOwner_surname",
              content: "$answerPostsContent",
              profilePicPath: "$answerPostsOwner_profilePicPath",
              date: "$answerPostsDate",
            },
          },
        },
      },
      {
        $addFields: {
          answerPosts: {
            $cond: [
              {
                $in: [{}, "$answerPosts"],
              },
              [],
              "$answerPosts",
            ],
          },
        },
      },
      {
        $match: {
          _id: {
            $ne: null,
          },
        },
      },
      {
        $sort: {
          date: -1,
          "answerPosts.date": 1,
        },
      },
    ]`

It works that way but I want to know if there is a simpler way to make this query.

0 Answers0