node.js - 使用 Mongoose findOneAndUpdate() 或 updateOne() 哪个模式选择子对象?
问题描述
环境: Node.js,猫鼬
我已经寻找了几个小时的答案,但我找不到与findOneAndUpdate()或updateOne()一起使用时选择子对象的模式。
我尝试了以下两种模式,但似乎都不起作用。
MyModel.findOneAndUpdate({ _id: value }, {parent: {child: 'hello world'});
MyModel.findOneAndUpdate({ _id: value }, {'parent.child': 'hello world'});
这个基本模式模式:
const MyModelSchema = new mongoose.Schema({
parent: {
child: {
type: String,
default: 'example text',
required: true
}
}
});
解决方案
你想念运营商 $set
MyModel.findOneAndUpdate({ _id: value }, {$set:{'parent.child': 'hello world'}});
推荐阅读
- go - args ...interface{} 对方法的参数到底意味着什么?
- ionic-framework - 连接到 Ionic Deploy 的 CORS 问题
- javascript - 如何组合多个正则表达式
- excel - 范围参数应用程序定义或对象定义错误
- arrays - 更新嵌套在数组中的多个级别的文档
- html - 将类添加到具有动态值的 div
- javascript - 在获取内容之前检查文件是否存在
- lisp - 定义原子函数
- google-cloud-dataflow - 处理卡在步骤中 将突变写入 Cloud Spanner 到 Spanner 而不在状态过程中输出或完成
- laravel - 提交故事后获得积分