javascript - Mongoose 更新不在架构中的属性
问题描述
我正在尝试设置一个 CRUD 应用程序,它允许人们将某些服务标记为完整或不完整。
随着时间的推移,服务可能会发生变化,有些是添加的,有些是删除的,所以我试图弄清楚我是否可以从架构中删除服务,但保留旧文档更新这些服务的能力。
这是我的架构的相关部分:
services: {
tom: {
name: {
type: String,
default: "Top of Mind",
required: true
},
status: {
type: Boolean,
default: false,
required: true
}
},
two: {
name: {
type: String,
default: "Service Two",
required: true
},
status: {
type: Boolean,
default: false,
required: true
}
},
three: {
name: {
type: String,
default: "Service Three",
required: true
},
status: {
type: Boolean,
default: false,
required: true
}
}
}
我将 strict 属性设置为 false 以希望更新任何其他服务,即使它不存在于架构中。例如,在 Mongo Atlas 集合中,我手动将服务添加到一个文档,如下所示:
test: {
name: "Test Service",
status: false
}
这在读取数据时工作正常,并且在对请求执行更新直到我尝试更改 test.status 时出现。
返回整个文档,包括服务下的测试对象,但doc.services.test
未定义,这意味着我无法编辑“测试”的状态。
编辑:这是查找文档时返回的服务对象:
{
tom: { name: 'Top of Mind', status: true },
two: { name: 'Service Two', status: true },
three: { name: 'Service Three', status: true },
test: { name: 'Service Test', status: false }
}
doc.services.test 返回未定义
这里是否有解决方法,或者“测试”服务是否需要存在于模式中才能更新它?
解决方案
推荐阅读
- javascript - 如何在Javascript中获取不带引号的输入值?
- vue.js - Vue.js 组件的创建顺序
- batch-file - 将参数传递给执行 DB2 命令的批处理文件
- python - 使用 python 语言将像 {1:23, 2:45, 3:17} 这样的小字典插入 Postgres 中的 SQL 数据库表的列中
- python - 创建一个字典列表,其中值是一个列表
- pandas - 将对象/字符串类型日期格式转换为日期时间类型时出现问题
- serial-port - 为什么串口不见了?
- c# - 带有 Blazor 和 MongoDB 的身份框架:登录时出错:无法解析类型“Microsoft.AspNetCore.Identity.SignInManager”的服务
- flutter - 当我尝试在我的 Mac m1 air 的终端中运行“flutter doctor --android-licenses”时出现此错误
- python - 如何在 Tkinter 中向根窗口添加高度?