【问题标题】:Get last conversations as admin in mongodb在 mongodb 中以管理员身份获取最后一次对话
【发布时间】:2022-02-09 06:38:04
【问题描述】:

我有一个学习管理系统 (LMS),学生可以在其中询问/与管理员聊天。

作为管理员,我想从仪表板中按 createdAt 和 seenByAdmin 排序的所有课程中获取最后一次看不见的聊天。

这是我的消息模型:

{
  studentId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Student"
  },
  lessonId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Lesson"
  },
  from: {
    type: String,
    enum: ["student", "admin"]
  },
  content: {
    type: String
  },
  seenByAdmin: {
    type: Boolean,
    default: false
  },
  createdAt: {
    type: Date,
    default: Date.now()
  }
}

你可以看到我不在乎哪个管理员在回复学生。

我想要的输出:

[
{
  lessonId: ObjectID,
  chats: [
    {
       studentId: ObjectID,
       createdAt: Date, /* of last message */
       seenByAdmin: Boolean, /* of last message */
       content: String /* content of last message (optional) */
    }
  ]
},
{
  lessonId: ObjectID,
  chats: [
    {
       studentId: ObjectID,
       createdAt: Date, /* of last message */
       seenByAdmin: Boolean, /* of last message */
       content: String /* content of last message (optional) */
    },
    {
       studentId: ObjectID,
       createdAt: Date, /* of last message */
       seenByAdmin: Boolean, /* of last message */
       content: String /* content of last message (optional) */
    }
  ]
}
]

[
  {
    lessonId: ObjectID,
    studentId: ObjectID,
    createdAt: Date, /* of last message */
    seenByAdmin: Boolean, /* of last message */
    content: String /* content of last message (optional) */
  },
  {
    lessonId: ObjectID,
    studentId: ObjectID,
    createdAt: Date, /* of last message */
    seenByAdmin: Boolean, /* of last message */
    content: String /* content of last message (optional) */
  }
]

我一直在编写一个 mongodb 聚合/查询,如果有任何帮助,我将不胜感激!

【问题讨论】:

    标签: node.js database mongodb performance nosql


    【解决方案1】:

    聚合管道:

    {
          $sort: {
            seenByAdmin: 1,
            createdAt: -1
          }
        },
        {
          $group: {
            _id: "$studentId",
            messages: {
              $first: "$$ROOT"
            }
          }
        },
        {
          $replaceRoot: {
            newRoot: "$messages"
          }
        },
        {
          $skip: skip || 0
        },
        {
          $limit: limit || 10
        },
        {
          $lookup: {
            from: "students",
            localField: "studentId",
            foreignField: "_id",
            as: "student"
          }
        },
        {
          $project: {
            studentId: 1,
            lessonId: 1,
            from: 1,
            content: { $substr: ["$content", 0, 150] },
            createdAt: 1,
            seenByAdmin: 1,
            student: {
              $arrayElemAt: ["$student", 0]
            }
          }
        },
        {
          $project: {
            studentId: 1,
            lessonId: 1,
            from: 1,
            content: 1,
            createdAt: 1,
            seenByAdmin: 1,
            student: {
              _id: 1,
              name: 1
            }
          }
        }
    

    【讨论】:

      猜你喜欢
      • 2014-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-23
      • 2012-11-22
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多