0

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")})

1 Answer 1

1
Group.find({members: req.body.uid}, function(err, items){})
Sign up to request clarification or add additional context in comments.

3 Comments

This was my first attempt - so I'm thinking there might be something else at play here. It's still not working. If I type this query in a mongo console, and use the ObjectId("......") syntax.. it returns.. so it's correct, but my type is not matching up for some reason for the equals or whatever it does internally for mongo.
I added my expanded code to the original example - have a look and let me know if I'm missing something stupid
Callback is invalid, function(err,items), not function(items). First parameter is error, second - returned array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.