1

I'm using Mongoosewith NodeJS.

I have these two schemas:

var feedSchema = mongoose.Schema({
    users: [{ type: Schema.Types.ObjectId, ref: 'User' }]
    ...
    ...
});

and

var userSchema = mongoose.Schema({
    email: String,
    ...
    ...
});

I want to find all feeds that contains the user._id inside its users field.

I'm wondering if mongoose has something like this:

Feed
.find({
    users : {
        $has : [
            user._id
        ]
    }
})
.exec(function(err, retData) { /* SOMETHING */ });

I couldn't find anything like that in the docs.

Thanks!

1 Answer 1

1

Try this:

Feed.find( { users: user._id } ).exec(...)
Sign up to request clarification or add additional context in comments.

1 Comment

Almost! I've tried { users : user._id } and it worked! Mongoose search inside the array automatically! docs.mongodb.org/manual/tutorial/query-documents/…

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.