Saturday, 8 April 2017

Search in a reference text field with Mongoose

I have this Schema

var users = new Schema({
    name: {type: String, required: true},
    email: {type: String, required: true},
    password: {type: String, required: true},
    following: [{type: Schema.ObjectId, ref: "users"}]
});
users.index({name: 'text'});

I want to use Mongoose to find users who they have "john" in their name and they are exists in following array of user of _id = x

in other way if it was SQL the query would be (it just example to illustrate the relations)

SELECT * FROM users where _id = x AND users.following.name LIKE '%john%'

I think that if following array was embedded in the user collections it would be easy to make.

How can i handle that in mongoose ?



via Moustafa Elkady

No comments:

Post a Comment