. 集合航班样本:
{
"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_2
and的分数Description_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 } } );
});
});
});
});
请问我怎样才能达到上述目的?