Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

431
Views
Saving an array in Mongoose save

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!