我想从users
数组中获取指定字段的值。类似的东西{_id: {$in: this.users}}
。不同之处在于它users
是一个对象数组,我想获取_id
该数组中每个对象的字段。这是一个代码示例:
var UserSchema = new Schema({
username: {type: String, required: true, unique: true},
password: {type: String, required: true,},
email: {type: String, required: true, unique: true},
role: {type: String, enum: ['user'], required: true},
name: String,
phoneNumber: Number,
companies: [{
_company: {type: Schema.ObjectId, ref: 'Company'},
points: Number
}],
points: Number,
created_at: Date,
updated_at: Date
})
我想为 mongoose UserSchema 编写这个中间件:
UserSchema.pre('remove', function(next) {
this.model('Company').update(
{_id: {$in: ??????????????????}},
{$pull: {users: this._id}},
next()
)
})
但我不知道如何_company
使用$in
.
任何帮助将不胜感激。谢谢。