在 Mongoose save 中保存数组 [英] Saving an array in Mongoose save

查看:89
本文介绍了在 Mongoose save 中保存数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要有关 Mongoose 数组的帮助.这是我的架构的外观:-

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,
  }],
})

Alert_Activities 是来自我的上游节点 js 应用程序的数组.我实现了 fswatch 功能,一旦特定文件发生更改,我希望将记录保存在我的收藏中.上游文件将始终包含一个数组.一般平均大约有 4 到 5 条记录.简而言之,数组中的每个元素都会有 Alert_Activities.

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.

我正在运行 for 循环,然后尝试将所有四个元素合二为一地保存到我的集合中.myObject 是使用 fs.read 从上游文件读取的完整数组

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")
            }
          })

Alert_Activities 显然不会保存.在猫鼬中这样做的正确方法是什么?

The Alert_Activities will obviously not save. What is the right way to do this in Mongoose?

推荐答案

如果您正在处理 Mongoose 文档,而不是 .lean() javascript 对象,您可能需要标记数组通过 markModified 然后才使用 .save().

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().

另外,directModifiedPaths 可以帮你查看文档中哪些字段被修改了.

我注意到您的 Alert_Activities 实际上是一个对象数组,因此请确保结果 mongoose 文档,您正在尝试 .save()满足所有验证规则.

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.

如果某些字段的更改成功保存,而其他字段的更改 - 不要,那么字段名称或验证肯定有问题.并且 DB/mongoose 不会触发您的错误,因为文档已经保存,尤其是.

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.

这篇关于在 Mongoose save 中保存数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆