猫鼬获取不包含子文档中某些项目的集合

mongoose get collections that not includes certain item in sub document

这是我的架构

const CourseSchema = new mongoose.Schema({
    title: {
    },
    cost: {
    },
    students: [
        {
            _id: ObjectId,
            fullName: String,
            email: String
        }
    ]
});

我有学生 John DoeobjectId: "something" ,在猫鼬中,我怎样才能获得不包括 john doe 的课程?用简单的话给他看他不买的课程。

您可以简单地使用 $ne 运算符来执行此操作:

db.collection.find({
  "students._id": {
    $ne: "id123"
  }
})

我在 mongoplayground 上为您创建了一个示例:https://mongoplayground.net/p/t9AyVVT9mAX