如何在猫鼬中更新嵌套数组值

Posted

技术标签:

【中文标题】如何在猫鼬中更新嵌套数组值【英文标题】:How to update nested array value in mongoose 【发布时间】:2017-05-19 22:24:30 【问题描述】:

如何使用 express 和 mongoose 更新嵌套数组值,其中是多维数组,需要更新单个对象键值。这是我的数据模型


"_id" : ObjectId("5847b92a20352925039432de"),
"user" : 
    "email" : "email@mail.io",
    "fullName" : "Name"

"availabilities" : [
    [
        
            "status" : "Close",
            "end" : "03:00",
            "start" : "00:00",
            "day" : "Sunday",
            "id" : "Sunday00:00"
        ,
        
            "status" : "Close",
            "end" : "06:00",
            "start" : "03:00",
            "day" : "Sunday",
            "id" : "Sunday03:00"
        ,
        
            "status" : "Open",
            "end" : "09:00",
            "start" : "06:00",
            "day" : "Sunday",
            "id" : "Sunday06:00"
        ,
        
            "status" : "Open",
            "end" : "12:00",
            "start" : "09:00",
            "day" : "Sunday",
            "id" : "Sunday09:00"
        ,
        
            "status" : "Open",
            "end" : "15:00",
            "start" : "12:00",
            "day" : "Sunday",
            "id" : "Sunday12:00"
        ,
        
            "status" : "Open",
            "end" : "18:00",
            "start" : "15:00",
            "day" : "Sunday",
            "id" : "Sunday15:00"
        ,
        
            "status" : "Open",
            "end" : "21:00",
            "start" : "18:00",
            "day" : "Sunday",
            "id" : "Sunday18:00"
        ,
        
            "status" : "Open",
            "end" : "24:00",
            "start" : "21:00",
            "day" : "Sunday",
            "id" : "Sunday21:00"
        
    ],
    [
        
            "status" : "Close",
            "end" : "03:00",
            "start" : "00:00",
            "day" : "Monday",
            "id" : "Monday00:00"
        ,
        
            "status" : "Close",
            "end" : "06:00",
            "start" : "03:00",
            "day" : "Monday",
            "id" : "Monday03:00"
        ,
        
            "status" : "Open",
            "end" : "09:00",
            "start" : "06:00",
            "day" : "Monday",
            "id" : "Monday06:00"
        ,
        
            "status" : "Open",
            "end" : "12:00",
            "start" : "09:00",
            "day" : "Monday",
            "id" : "Monday09:00"
        ,
        
            "status" : "Open",
            "end" : "15:00",
            "start" : "12:00",
            "day" : "Monday",
            "id" : "Monday12:00"
        ,
        
            "status" : "Open",
            "end" : "18:00",
            "start" : "15:00",
            "day" : "Monday",
            "id" : "Monday15:00"
        ,
        
            "status" : "Open",
            "end" : "21:00",
            "start" : "18:00",
            "day" : "Monday",
            "id" : "Monday18:00"
        ,
        
            "status" : "Open",
            "end" : "24:00",
            "start" : "21:00",
            "day" : "Monday",
            "id" : "Monday21:00"
        
    ]
]

在这里,我想更改可用性数组中的第一个索引,然后更改第一个索引对象状态键值。 即: "status" : "Close", "end" : "03:00", "start" : "00:00", "day" : "Sunday", "id" : "Sunday00:00"

【问题讨论】:

描述此架构的 model.js 的名称是什么?? 【参考方案1】:

您可以使用positional $ operator 或索引号来定位数组条目。

db.students.update(
    _id: 4, "grades.grade": 85 ,
    $set:  "grades.$.std" : 6  
)

【讨论】:

请用我的模型举个例子,因为我没有固定索引,它会根据场景动态变化。 @OsmanGoniNahid 您需要事先知道要查找的索引。如果这样做,只需使用索引号即可。 这种情况有没有办法使用Mongoose动态查找索引?【参考方案2】:
yourMoongosemodel.findByIdAndUpdate(id : giveId, 
  $push: 
         availabilities[0].status : 'open'
          
 , new : true, function(err, data)
 console.log(data);
   )

【讨论】:

通常最好在您的帖子中添加解释代码如何工作的说明。这使新开发人员能够了解代码的作用。

以上是关于如何在猫鼬中更新嵌套数组值的主要内容,如果未能解决你的问题,请参考以下文章

如何仅在猫鼬中使用聚合填充嵌套在对象数组中的字段?

如何在猫鼬中保存子文档的数组?

如何在猫鼬中填充嵌套实体?

如何在猫鼬中异步填充嵌套对象?

如何使用 $elemMatch 查找和过滤并更新数组的所有元素该元素在猫鼬中有一个特殊的日期?

如何在猫鼬中动态地将值推入对象的属性?