使用Mongoose

时间:2016-09-04 10:27:30

标签: javascript node.js mongodb mongoose field-names

收集样本航班

 {
        "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 } } );        
});
});
});
});

请问我怎样才能实现上述目标?

1 个答案:

答案 0 :(得分:0)

正确的代码应该是:

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