使用Mongoose的MongoDB中的动态字段名称



收集样本航班

 {
        "Orig" : "AGP",
        "Dest" : "CMN",
        "Description_Flight" : "One-Stop-Narrow Type",
        "Description_Flight_2" : "WESTERN EUROPE/WESTERN EUROPE",
        "Mkt_Al" : "0B"
  }

采集样本Coeff

{
    "Regions" : "WESTERN EUROPE/WESTERN EUROPE",
    "Non-Stop-Narrow Type" : 2.4109,
    "Non-Stop-Supersonic" : 2.71828,
    "One-Stop-Narrow Type" : 2.22554,
    "One-Stop-Turbo" : 0.92312,
    "One-Stop-Wide Type" : 11.24586,
    "One-Stop Online-Turbo" : 0.07577
}

我想要什么

我有我的起始集合Flights,我想为每个文档打分,一个基于Description_Flight_2Description_Flight的分数。

示例:

例如,在我的样本中,我有:

        "Description_Flight" : "One-Stop-Narrow Type",
        "Description_Flight_2" : "WESTERN EUROPE/WESTERN EUROPE",

所以,我应该去Coeff集合,找到区域:

          "WESTERN EUROPE/WESTERN EUROPE"

然后取适当的值,这里我应该取这行的值:

        "One-Stop-Narrow Type" : 2.22554,

我试过这个:

    mongoose.connect('mongodb://localhost/mydb');
var cSchema = new Schema({},{ strict: false, collection: 'flights' }),    
    dflights = mongoose.model("flights", cSchema);
var rSchema = new Schema({},{ strict: false, collection: 'coeff' }), 
    coeff = mongoose.model("coeff", rSchema);
    mongoose.set('debug', false);
    mongoose.connection.on("open", function (err) { 
    if (err) throw err;  

    dflights.find({}).lean().exec(function (err, flights) {  
    if (err) throw err; 
    flights.forEach(function(flight) { 
var Flight_Description = "", score =0 ;
    coeff.findOne({Regions : flight.Description_Flight_2}).lean().exec(function (err, coef) {

这里是重要的行:

    Flight_Description = flight.Description_Flight;
    score = (coef != null ) ?  coef.Flight_Description : "Missed data";

这里是最后几行

if ( score != 0) 
    dflights.collection.update({_id:flight._id}, { $set : { score :score } } );        
});
});
});
});

请问我该如何实现上述目标?

正确的代码应该是:

score = (coef != null ) ?  coef[Flight_Description] : "Missed data";

相关内容

  • 没有找到相关文章

最新更新