【问题标题】:In a Mongoose Schema return "parent" collection specific value在 Mongoose Schema 中返回“父”集合特定值
【发布时间】:2012-07-30 22:09:05
【问题描述】:

我需要的是从 Schema 方法返回一个特定的“父”值:

我有两个架构:

var IP_Set = new Schema({
    name: String
});

var Hash_IP = new Schema({
    _ipset      : {
        type: ObjectId,
        ref: 'IP_Set'
    },
    description : String
});

Hash_IP 模式中,我希望有以下方法:

Hash_IP.methods.get_parent_name = function get_parent_name() {
    return "parent_name";
};

所以当我跑步时:

var hash_ip = new Hash_IP(i);
console.log(hash_ip.get_parent_name())

我可以获得关联的Hash_IP 实例的IP_Set 名称值。

到目前为止,我有以下定义,但我无法返回名称:

Hash_IP.methods.get_parent_name = function get_parent_name() {
    this.model('Hash_IP').findOne({ _ipset: this._ipset })
        .populate('_ipset', ['name'])
        .exec(function (error, doc) {
            console.log(doc._ipset.name);
        });
};

我试过了:

Hash_IP.methods.get_parent_name = function get_parent_name() {
    this.model('Hash_IP').findOne({ _ipset: this._ipset })
        .populate('_ipset', ['name'])
        .exec(function (error, doc) {
           return doc._ipset.name;
        });
};

没有结果。

提前感谢您的帮助。

【问题讨论】:

    标签: node.js schema express return-value mongoose


    【解决方案1】:

    我相信你已经很亲近了。你的问题不是很清楚,但我认为

    .populate('_ipset', ['name'])
    .exec(function (error, doc) {
      console.log(doc._ipset.name);
    });
    

    正在工作并且

    .populate('_ipset', ['name'])
    .exec(function (error, doc) {
       return doc._ipset.name;
    });
    

    不是吗?

    很遗憾,异步 return 没有按照您希望的方式工作。

    .exec 调用您的回调函数,该函数返回名称。但是,这不会将名称作为 get_parent_name() 的返回值返回。那样就好了。 (想象一下return return name 语法。)

    像这样将回调传递给get_parent_name()

    Hash_IP.methods.get_parent_name = function get_parent_name(callback) {
        this.model('Hash_IP').findOne({ _ipset: this._ipset })
            .populate('_ipset', ['name'])
            .exec(callback);
    };
    

    您现在可以在代码中使用instance_of_hash_ip.get_parent_name(function (err, doc) { ... do something with the doc._ipset.name ... });

    奖励答案 ;)

    如果您经常使用父母的名字,您可能希望始终在初始查询中返回它。如果您将.populate(_ipset, ['name']) 放入查询Hash_IP 的实例,那么您将不必在代码中处理两层回调。

    只需将find()findOne(),然后是populate() 放入模型的一个不错的静态方法中。

    奖励答案的奖励示例:)

    var Hash_IP = new Schema({
        _ipset      : {
            type: ObjectId,
            ref: 'IP_Set'
        },
        description : String
    });
    
    Hash_IP.statics.findByDescWithIPSetName = function (desc, callback) {
        return this.where('description', new RegExp(desc, 'i'))
            .populate('_ipset', ['name']) //Magic built in
            .exec(cb)
    };
    
    module.exports = HashIpModel = mongoose.model('HashIp', Hash_IP);
    
    // Now you can use this static method anywhere to find a model 
    // and have the name populated already:
    
    HashIp = mongoose.model('HashIp'); //or require the model you've exported above
    HashIp.findByDescWithIPSetName('some keyword', function(err, models) {
       res.locals.hashIps = models; //models is an array, available in your templates
    });
    

    现在每个模型实例都已经定义了 models._ipset.name。享受:)

    【讨论】:

    • 感谢您的奖励回答! :) 您在第一个答案中指出的正是我所需要的。我想以一种不需要传递回调的方式执行此操作,因为您在 Hash_IP.methods.get_parent_name 方法的示例中指出,因为其他应用程序要求:(您可以发布您的奖励答案的奖励示例:D。提前致谢!
    • 我差点忘了:+1 奖励答案呵呵:D
    • 我接受了你的回答,因为我的问题在技术上是正确的,但我最终将 ipset_name 添加到 de Hash_IP 模式中作为父引用,并删除了 _ipset 变量。这样我就不会再受苦了哈哈哈 :D 谢谢。
    猜你喜欢
    • 2012-08-30
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    相关资源
    最近更新 更多