Mongoose:使用聚合基于Model.field_1.field_2进行过滤不起作用

u4vypkhs  于 2023-03-12  发布在  Go
关注(0)|答案(1)|浏览(103)

我有这个功能:

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
  }
};

我给它一个lounge_id,它搜索notification.lounge_join_request.lounge等于lounge_id的通知。
我编写了第二个函数,它拉取一个随机的notification,并获得它的lounge_join_request.lounge_id,然后将该lounge_id提供给第一个函数。

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);
  }
};

因为,我从一个已有的notification中提取了那个lounge_id,第一个函数至少应该返回一个包含那个通知的数组。
但是,它不会,它总是返回一个空数组

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);

知道为什么吗?
以下是相关的模型:
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);
k5ifujac

k5ifujac1#

在您的模式中,lounge_join_request字段包含ObjectId,而不是对象,因此没有要匹配的lounge_join_request.lounge字段。
如果lounge_id包含与休息室加入请求文档的_id匹配的内容,则可以使用

{
     $match: {
        lounge_join_request: lounge_id,
        lounge_join_request: { $ne: null },
     },
 }

相关问题