mongoose通过引用属性查找文档

时间:2016-03-24 13:45:36

标签: node.js mongodb mongoose

我有第一个模特人物:

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

第二个模特夫妇:

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

我发现一对人与ObjectId:

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

但我想通过给出一个名字而不是一个Person的ObjectId找到一对,就像这样:

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

但它总是空的......

无论如何要解决这个问题?

感谢您的任何反馈。

修改

我刚刚实现了答案:

现在让我们看看我的情侣模特:

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);

有了这种用法:

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

有效!感谢您的回答和编辑。

1 个答案:

答案 0 :(得分:6)

couple模型中,person1是一个ObjectID(我知道你知道),因此它显然没有属性.firstname

实际上,实现此目的的最佳方法是通过它的名字找到用户,然后使用用户的id查询这对夫妇。

这个方法可以/应该在couple模型中作为静态方法(简化代码示例):

couple.statics.findByPersonFirstname = function (firstname, callback) {
  var query = this.findOne()

  Person.findOne({firstname: firstname}, function (error, person) {
    query.where($or: [
      {person1: person._id},
      {person1: person._id}
    ]).exec(callback);
  })

  return query
}

只需like this exemple

编辑:另请注意,引用必须是_id(因此您无法使用名字进行存储,无论如何这都是个坏主意。)

考虑您的编辑:

Person._id可能是String,引用是ObjectId,如果是,请尝试:

{person1: mongoose.Types.ObjectId(Person._id)}

此外,您的变量为person而非Person。尝试记录person以查看是否有所收获。

最后,我的代码示例非常简单,不要忘记处理错误和所有(请参阅上面给出的链接,这是完整的)。