Need help with arrays in Mongoose. Here is how my schema looks :-
const alertsSchema = new mongoose.Schema({
Alert_Date: String,
Alert_StartTime: String,
Alert_EndTime: String,
Alert_RuleName: String,
Alert_RuleId: String,
Alert_EntryNumber: String,
Alert_AlertId: String,
Alert_Description: String,
Alert_TriggerTime: String,
Alert_Activities: [{
Alert_Activities_ActivityType: String,
Alert_Activities_ActivityTime: String,
Alert_Activities_AreaName: String,
Alert_Activities_AreaType: String,
Alert_Activities_Position: Array,
Alert_Activities_Duration: Number,
Alert_Activities_SecondVesselId: String,
Alert_Activities_SecondVesselName: String,
Alert_Activities_SecondVesselClassCalc: String,
Alert_Activities_SecondVesselSize: String,
Alert_Activities_SecondVesselMMSI: String,
Alert_Activities_SecondVesselIMO: String,
}],
})
The Alert_Activities is an array coming from my upstream node js application. I implemented a fswatch functionality and as soon as a particular file changes, I am looking to save the record in my collection. The upstream file will always contain an array. Generally on an average of around 4 to 5 records. In short Alert_Activities will be there for every element of the array.
I am running a for loop and then trying to save all four elements in one go into my collection.myObject is the full array read from the upstream file using fs.read
for(i=0; i<myObject.length; i++){
var newAlertData = new alertRegister({
Alert_Date: date1,
Alert_StartTime: startNotificationDate,
Alert_EndTime: endNotificationDate,
Alert_RuleName: myObject[i].ruleName,
Alert_RuleId: myObject[i].ruleId,
Alert_EntryNumber: myObject[i].entryNumber,
Alert_AlertId: myObject[i].alertId,
Alert_Description: myObject[i].description,
Alert_TriggerTime: myObject[i].triggerTime,
// Alert_Activities: myObject[i].activities,
});
newAlertData.save(function(err,data1){
if(err){
console.log(err)
} else {
console.log("data saved")
}
})
The Alert_Activities will obviously not save. What is the right way to do this in Mongoose?