Day 170 of Learning MERN Stack

์ž‘์„ฑ์ž

์นดํ…Œ๊ณ ๋ฆฌ:

โ† ํ”ผ๋“œ๋กœ
DEV Community ยท Ali Hamza ยท 2026-08-01 ๊ฐœ๋ฐœ(SW)

Ali Hamza

Hello Dev Community! ๐Ÿ‘‹

It is officially Day 170 of my full-stack engineering track! Today, I designed and implemented the backend message retrieval and chat history processing endpoints (getUserforSidebar & getMessages) for QuickChat using Node.js, Express, and MongoDB! โš™๏ธ๐Ÿ’ฌ

Here is how I structured the database queries for chat retrieval and read receipts.

๐Ÿ› ๏ธ Technical Breakdown: Message Fetching & Unseen Counters

As captured in my implementation snapshots (messageController.js):

1. Dynamic User Discovery & Parallel Unseen Counting

  • Filtered out the currently logged-in user using Mongoose $ne operator while stripping out password projections.
  • Used Promise.all alongside map iterations to asynchronously gather unread message counts per user card:

javascript
  const filteredUser = await user.find({ _id: { $ne: userId } }).select("-password");

  const promise = filteredUser.map(async (user) => {
    const messages = await message.find({ senderId: user._id, receiverId: userId, seen: false });
    if (messages.length > 0) {
      unseenMessages[user._id] = messages.length;
    }
  });
  await Promise.all(promise);

Enter fullscreen mode Exit fullscreen mode

์›๋ฌธ์—์„œ ๊ณ„์† โ†—

์ถ”์ถœ ๋ณธ๋ฌธ ยท ์ถœ์ฒ˜: dev.to ยท https://dev.to/ali_hamza_589ec7b3eb6688d/day-170-of-learning-mern-stack-53ck

์ฝ”๋ฉ˜ํŠธ

๋‹ต๊ธ€ ๋‚จ๊ธฐ๊ธฐ

์ด๋ฉ”์ผ ์ฃผ์†Œ๋Š” ๊ณต๊ฐœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•„์ˆ˜ ํ•„๋“œ๋Š” *๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค