查询 condition/operator: 其中 id 不在数组中 (NIN)

Query condition/operator: Where id is not in array (NIN)

我有一个 Object 的模式,其中有一个字段

helpOffers : [{ type: Schema.ObjectId, ref: 'User' }]

包含很多用户id。

我想找到给定 ID loggedInUserId 不在 helpOffers 内的所有对象。

我尝试了 ObjectSchema.where('helpOffers').nin(loggedInUserId)...,但这不起作用。

因为 loggedInUserId 是单个值而不是数组,因此您可以使用 $ne 运算符而不是 $nin 来实现:

ObjectSchema.where('helpOffers').ne(loggedInUserId)...

附带说明一下,ObjectSchema 是您的模型的混淆名称。最好让您的架构和模型命名清晰明了。