Question

I have the following schema:

var groupSchema = mongoose.Schema({
name: {type: String, index: {unique: true, dropDups: true}},
createdBy: String,
availableModules: [Number],
members: [{type: Schema.Types.ObjectId, ref: 'User'}]
});

The "members" field is just a straight up array of ObjectID's from the user table. I need to check for the existence of a given ObjectId (user._id) in the members subdocument collection before I take some other action.

I've tried stuff like:

Group.find({req.body.uid}, {$in: {"members"}}, function(grps){..});

But this doesn't work. I've tried a myriad of other methods but can't seem to get anything working. I'd think this would be easy, but I can't seem to figure it out.

EDIT Further Investigation:

User.findOne({"local.email": req.body.user.email}, function(err, user){
    Group.find({"members": user._id}, function(grps){
        if(grps){ //do something}
    });
});

grps is null no matter what way I do it.. tho in the mongo console it's returning the value when using the ObjectId("..") notation

db.groups.find({members: ObjectId("5371a4763b32c3620728acb5")})
Was it helpful?

Solution

Group.find({members: req.body.uid}, function(err, items){})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top