Tuesday, 14 March 2023

Mongoose: Using aggregate to filter based on Model.field_1.field_2 doesn't work

I have this function:

const getNotificationsForLounge = async (lounge_id) => {
  try {
    const notifications = await Notification.aggregate([
      {
        $match: {
          "lounge_join_request.lounge": lounge_id,
          lounge_join_request: { $ne: null },
        },
      },
    ]);
    console.log("🚀 ~ notifications:", notifications);
    // Do something with the notifications
    return notifications;
  } catch (error) {
    console.log("🚀 ~ error:", error);
    // Handle the error
  }
};

I feed it a lounge_id and it searchs the notifications where notification.lounge_join_request.lounge is equal that lounge_id.

I wrote a second function that pulls a random notification and gets its lounge_join_request.lounge_id and then feeds that lounge_id to the first function.

const getNotificationWithRandomLoungeJoinRequest = async () => {
  try {
    const notification = await Notification.aggregate([
      // Match notifications with lounge join requests
      {
        $match: {
          category: "LOUNGE_JOIN_REQUEST",
          lounge_join_request: { $ne: null },
        },
      },
      // Select a random notification using $sample
      { $sample: { size: 1 } },
      {
        $lookup: {
          from: "lounge_join_requests",
          localField: "lounge_join_request",
          foreignField: "_id",
          as: "lounge_join_request",
        },
      },
      // Project the lounge ID
      { $project: { _id: 0, lounge_id: "$lounge_join_request.lounge" } },
    ]);
    const lounge_id = notification[0].lounge_id[0];
    // Logs a lounge id
    // 🚀 ~ file: loungeServices.js:663 ~ getNotificationWithRandomLoungeJoinRequest ~ lounge_id: 63ef344xxxxb4943355
    // Which means there exists a notification
    // Where notification.lounge_join_request.lounge equals this lounge_id
    console.log(
      "🚀 ~ file: loungeServices.js:663 ~ getNotificationWithRandomLoungeJoinRequest ~ lounge_id:",
      lounge_id
    );

    // But, when I feed that lounge_id into this function
    // Which searchs notifications where
    // notification.lounge_join_request.lounge equals this lounge_id
    // It logs an empty array
    return await getNotificationsForLounge(lounge_id);
  } catch (error) {
    console.log("🚀 ~ error:", error);
  }
};

Since, I pulled that lounge_id from an existing notification. The first function should at least return an array containing that notification.

But, it doesn't, it always returns an empty array. Focus on this part:

 const lounge_id = notification[0].lounge_id[0];
    // Logs a lounge id
    // 🚀 ~ file: loungeServices.js:663 ~ getNotificationWithRandomLoungeJoinRequest ~ lounge_id: 63ef344xxxxb4943355
    // Which means there exists a notification
    // Where notification.lounge_join_request.lounge equals this lounge_id
    console.log(
      "🚀 ~ file: loungeServices.js:663 ~ getNotificationWithRandomLoungeJoinRequest ~ lounge_id:",
      lounge_id
    );

    // But, when I feed that lounge_id into this function
    // Which searchs notifications where
    // notification.lounge_join_request.lounge equals this lounge_id
    // It logs an empty array
    return await getNotificationsForLounge(lounge_id);

Any idea why?


These are the relevant models:

Notification.js

const NotificationSchema = new Schema({
  lounge_join_request: {
    type: Schema.Types.ObjectId,
    ref: "lounge_join_requests",
    default: null,
  },
});

module.exports = Notification = mongoose.model(
  "notification",
  NotificationSchema
);

LoungeJoinRequest.js

const LoungeJoinRequestSchema = new Schema({
  lounge: {
    type: Schema.Types.ObjectId,
    ref: "lounge",
    required: true,
  },
});

module.exports = LoungeJoinRequest = mongoose.model(
  "lounge_join_requests",
  LoungeJoinRequestSchema
);

Lounge.js

const LoungeSchema = new Schema(
  {
    name: {
      type: String,
    },
  }
);

module.exports = Channel = mongoose.model("lounge", LoungeSchema);


from Mongoose: Using aggregate to filter based on Model.field_1.field_2 doesn't work

No comments:

Post a Comment