Saving an array in Mongoose save

Viewed 409

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?

1 Answers

If you are dealing with Mongoose Documents, not with .lean() javascript objects, probably, you'll need to mark array field(s) as modified via markModified and only then use .save().

Also, directModifiedPaths could help you to check, what fields has been modified in the document.

I have noticed that your Alert_Activities, is actually an array of objects, so make sure that the result mongoose documents, which you are trying to .save() really satisfy all the validation rules.

If changes on some fields do successfully saved but the others - don't, then if's definitely something wrong with field names, or validation. And the DB/mongoose doesn't trigger you an error, because the document has already been saved, even in particular.

Related