mongoose find a document by reference property

Viewed 25853

I have a first model Person:

var personSchema    = new Schema({
    firstname: String,
    name: String
});
module.exports = mongoose.model('Person', personSchema);

And a second model Couple:

var coupleSchema    = new Schema({
    person1: [{ type: Schema.Types.ObjectId, ref: 'Person' }],
    person2: [{ type: Schema.Types.ObjectId, ref: 'Person' }]
});
module.exports = mongoose.model('Couple', coupleSchema);

I find a couple with a person ObjectId:

    Couple.find({
        'person1': req.params.objectid
    })
    .populate({
        path: 'person1 person2'
    })
    .exec(function (err, couple) {
        if (err)
            res.send(err);
        res.json(couple)
    });

But I would like to find a couple by giving a firstname and not an ObjectId of a Person, something like that:

    Couple.find({
        'person1.firstname': "Bob"
    })
    .populate({
        path: 'person1 person2'
    })
    .exec(function (err, couple) {
        if (err)
            res.send(err);
        res.json(couple)
    });

But it is always empty...

Anyway to solve this?

Thank you for any feedback.

EDIT

I just implemented the answer:

Let's see my Couple model now:

var Person      = require('mongoose').model('Person');
var coupleSchema    = new Schema({
    person1 : [{ type: Schema.Types.ObjectId, ref: 'Person' }],
    person2 : [{ type: Schema.Types.ObjectId, ref: 'Person' }]
});

coupleSchema.statics.findByUsername = function (username, callback) {
  var query = this.findOne()

  Person.findOne({'firstname': username}, function (error, person) {
    query.where(
      {person1: person._id}
    ).exec(callback);
  })
  return query
}

module.exports = mongoose.model('Couple', coupleSchema);

With this usage:

Couple.findByUsername(req.params.username, function (err, couple) {
        if(err)
            res.send(err);
        res.json(couple);
    });

That works! Thank you for your answer and edits.

1 Answers
Related