I want to get values of specified field from the users array.
Something like {_id: {$in: this.users}}. The difference is that users is an array of objects and I want to get the _id field of each object in this array.
Here's a code sample:
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
})
And I want write this middleware for mongoose UserSchema:
UserSchema.pre('remove', function(next) {
this.model('Company').update(
{_id: {$in: ??????????????????}},
{$pull: {users: this._id}},
next()
)
})
But I don't know how to retrieve _company field from companies using $in.
Any help will be appreciated. Thanks.
usersfield ofUserSchema?