MongoDB 中使用 Mongoose 的动态字段名称
Dynamic field names in MongoDB using Mongoose
. collection 航班示例 :
{
"Orig" : "AGP",
"Dest" : "CMN",
"Description_Flight" : "One-Stop-Narrow Type",
"Description_Flight_2" : "WESTERN EUROPE/WESTERN EUROPE",
"Mkt_Al" : "0B"
}
. collection 的样本 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
}
我想要什么?
我有我的起始 collection、Flights
并且我想为每个文档打分,一个基于 Description_Flight_2
和 Description_Flight
的分数。
示例:
例如,在我的示例中我有:
"Description_Flight" : "One-Stop-Narrow Type",
"Description_Flight_2" : "WESTERN EUROPE/WESTERN EUROPE",
所以,我应该去 Coeff
collection,然后找到区域 :
"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";
. collection 航班示例 :
{
"Orig" : "AGP",
"Dest" : "CMN",
"Description_Flight" : "One-Stop-Narrow Type",
"Description_Flight_2" : "WESTERN EUROPE/WESTERN EUROPE",
"Mkt_Al" : "0B"
}
. collection 的样本 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
}
我想要什么?
我有我的起始 collection、Flights
并且我想为每个文档打分,一个基于 Description_Flight_2
和 Description_Flight
的分数。
示例:
例如,在我的示例中我有:
"Description_Flight" : "One-Stop-Narrow Type",
"Description_Flight_2" : "WESTERN EUROPE/WESTERN EUROPE",
所以,我应该去 Coeff
collection,然后找到区域 :
"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";