我想從users
數組中獲取指定字段的值。 類似於{_id: {$in: this.users}}
。不同的是,users
是一個對象數組,我想獲取此數組中每個對象的_id
字段。 下面是一個代碼示例:如何獲取mongodb中的對象數組中的指定字段的值
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
})
而且我想寫這中間件貓鼬UserSchema:
UserSchema.pre('remove', function(next) {
this.model('Company').update(
{_id: {$in: ??????????????????}},
{$pull: {users: this._id}},
next()
)
})
但我不知道如何使用$in
公司取回_company
場。
任何幫助將不勝感激。謝謝。
哪裏''UserSchema'的users'領域? – zangw
Sory,複製粘貼後我沒有改變它。它應該是另一個Schema。 – kuba12