Mongodb mongoose查询,其中id不在其他模型中

Mongodb mongoose查询,其中id不在其他模型中,mongodb,mongoose,mongodb-query,aggregation-framework,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,所以我有这种模型 const vetSchema = new mongoose.Schema({ username: {type: String, required: true, trim: true, unique: true}, email: {type: String, unique: true, trim: true,}, expYear: {type: Number}, KTP: {type: String, unique: true, trim: tr

所以我有这种模型

const vetSchema = new mongoose.Schema({
    username: {type: String, required: true, trim: true, unique: true},
    email: {type: String, unique: true, trim: true,},
    expYear: {type: Number},
    KTP: {type: String, unique: true, trim: true},
    cert_id: {type: String, unique: true, trim: true, select: false},
})

const clinicSchema = new mongoose.Schema({
    vet: [{type: mongoose.Schema.Types.ObjectID, ref: 'vet'}],
)}
我想做的是通过用户名查找vet,基于诊所ID,诊所中不包括vet ID,查找vet的查询是
{username:{$regex:(?I)${keyword}.*}

例如,我们有这些数据

Clinic = [{
    "_id" : ObjectId("5e57a45836d300b188f4444a"),
    "vet": [ObjectId("5e0742fc9d4b20100f89b626"),ObjectId("5e53698fd2b9ee43e7693e01")]
}]

Vet = [
{
    "_id" : ObjectId("5e0742fc9d4b20100f89b626"),
    "username": "VetClinic"
},
{
    "_id" : ObjectId("5e55df62576bc9811877033e"),
    "username": "VetNotClinic"
},
{
    "_id" : ObjectId("5e53698fd2b9ee43e7693e01"),
    "username": "VetClinic"
},
]

我想要的是,如果我找到
Vet
它只显示
VetNotClinic
,因为
VetNotClinic
包含在诊所的ID中,任何好的查询/agregate?

您可以使用
$nin
来查询字段“不在”这样的数组中的位置

Vet.find({
用户名:{$regex:(?i)${keyword}.*},
_id:{$nin:clinic.vet}
})

那么你的意思是
诊所
是模型吗?@ericantony clinic.vet是诊所集合/模型中特定诊所中的兽医数组