Ref with string in mongoose

I have two models

 var Passenger = mongoose.model('passengers', new Schema({
        username: {
            type: String,
            ref: 'users'
        },
        company: String,
        baggage: String,
        note: String,
        owner: String
    }));


    var User = mongoose.model('users', new Schema({
        username: String,
        first: String,
        last: String,
        email: String,
        password: String
    }));

      

I would really like to get a full user instead of a passenger with a username. I thought ref and populate would be the way to do this.

Trip.find({
        request: true
    }).populate('flights.users').exec(function (err, trips) {

});

      

The top-level document - "Trip" and "Flights" is an array under "Trips", and "Passengers" is an array under "Flights".

It's impossible. Should I give my users all IDs instead?

Thanks for the help.

+3


source to share





All Articles