Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

437
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda