I’ve a database scheme for Fleet Management system, as part of the system i’ve tables (or collections to use the proper MongoDB terminology) for static information, I’ve collection for vehicles that reference another collections of static information including make collections, the scheme looks like:
var mongoose = require("mongoose"),
Make = require("../models/make"),
Color = require("../models/color"),
Vehiclecat = require("../models/vehiclecategory"),
VehicleUsage = require("../models/vehicleusage"),
RegistrationCenter = require("../models/registration"),
FuelType = require("../models/fueltype"),
Insurance = require("../models/insurance");
var vehicleSchema = new mongoose.Schema({
stringId: String,
make: {
type: mongoose.Schema.Types.ObjectId, ref:"Make",
makeText: String
},
color:{
type: mongoose.Schema.Types.ObjectId,ref:"Color",
colorText: String
},
category:{
type: mongoose.Schema.Types.ObjectId, ref:"Vehiclecat",
categoryText: String
},
usage:{
type: mongoose.Schema.Types.ObjectId, ref:"VehicleUsage",
usageText: String
},
year: String,
registrationExpiry: Date,
plateNo: String,
registrationNo: String,
registrationCenter: {
type: mongoose.Schema.Types.ObjectId, ref:"RegistrationCenter",
registrationCenterText: String
},
vin:String,
engineNo: String,
fuelType: {
type: mongoose.Schema.Types.ObjectId, ref:"FuelType",
fuelTypeText: String
},
load: String,
insurance:{type: mongoose.Schema.Types.ObjectId, ref:"Insurance"}
},{strict:false});
module.exports = mongoose.model("Vehicle", vehicleSchema);
and the make scheme
var mongoose = require("mongoose");
var modelSchema = new mongoose.Schema({
name: String
});
var makeSchema = new mongoose.Schema({
name: String,
models: [modelSchema]
});
module.exports = mongoose.model("Make", makeSchema);
later i realized that i’m over complicating things by using a whole collection for information such as the vehicle make, since this data is often read rather than written to i decided to de-normalize the database and i’ve added new text field to vehicle scheme to represent the vehicle make (see field vehicle.make.makeText) and wrote a code to iterate through all vehicles collection then for each vehicle find the matching model id referenced in makes collection then copy the make.name to vehicle.make.makeText, below is the code that does the job:
// code to loop through all records in vehicles collection and add makeText
vehicle.find({},(err,allVehicles) =>{
console.log("executed vehicle.find()");
async.forEach(allVehicles,function(vehicle,callback){
console.log(vehicle.stringId);
make.findById(vehicle.make.id,function(err,foundMake){
vehicle.make.makeText || (vehicle.make.makeText);
vehicle.make.makeText = foundMake.name;
console.log(foundMake.name);
vehicle.save(function(err,saved){
if(err){
console.log(err.message);
}
console.log("executed vehicle.save" + saved);
console.log("make Text " + saved.make.makeText);
callback();
})
})
},function(err){
if (err){
console.log(err.message);
}
}
)});
the code seem to be working as i’m getting the expected results in the console, however, appreanlty for one reason or another it does ignore the new field vehicle.make.makeText
I’ve searched the web and used the proposed solutions such as adding {strict: false} to the vehicle scheme definition and it didn’t work.
Thanks in advance for your ideas.