Reputation: 112
I searched in all places and in the Mongoose's documentation without success.
I wanna search all content in my users collection that is from type 'Admin'.
UserTypeSchema = new Schema({
name: {
type: String,
required: true
},
user_type: [
{
type: Schema.Types.ObjectId,
ref: 'User'
}
],
}, {
strict: true
});
UserSchema = new Schema({
user_type: {
type: Schema.Types.ObjectId,
ref: 'UserType'
},
}, {
strict: true
});
Query to find users by type (being another model as reference)
User
.find()
.populate('user_type', 'name', null, {'user_type': 'Admin'})
.exec(function(err, users) {
res.send(users);
});
Thanks
Upvotes: 0
Views: 44
Reputation: 4238
I see two possiblities, either you query the type schema for admin and use the returned object to query for users like this:
Type.findOne({name: 'Admin'}, function(err, type) {
User.find({user_type: type._id}, function(err, users) {
...
})
})
Or if you have a reference in your type schema to users, couldn't you simply do:
Type.findOne({name: 'Admin'}, function(err, type) {
var users = type.user_type;
...
})
Or am I reading your schema wrong?
Upvotes: 1