mongodb - Mongoose 查询 - findByIdAndUpdate 覆盖其他嵌套字段
问题描述
// MODEL MONGOOSE
const programSchema = new mongoose.Schema({
name: { type: String, default: "" },
description: { type: String, default: "" },
accommodation: {
info: {
offer: { type: String, default: "" },
description: { type: String, default: "" },
completed: { type: Boolean, default: false }
},
arrangement: {
participant: { type: String, default: "" },
bedroom: { type: String, default: "" },
completed: { type: Boolean, default: false }
}
}
});
我的应用程序
我运行一次,它会更新。
Program.findByIdAndUpdate(pid,
{
$set: {
'accommodation.info.offer': "Yes",
'accommodation.info.description': "Something",
}
}
).then(() => Program.findById(pid)
.then((program) => {
console.log(program);
done();
}))
});
在我的第一个动作之后,我的数据如下所示:
{
name: "",
description: "",
accommodation: {
info {
offer: "Yes",
description': "Something"
completed: false
},
arrangement {
participant: "",
bedroom': ""
completed: false
}
}
}
我运行第二个操作,它更新但覆盖并重置上面的第一个:
Program.findByIdAndUpdate(pid, {
$set: {
"accommodation.arrangement.participant": "Tom",
"accommodation.arrangement.bedroom": "Jones"
}
}).then(() =>
Program.findById(pid).then(program => {
console.log(program);
done();
})
);
在我的第二次行动之后,我的数据如下所示:
{
name: "",
description: "",
accommodation: {
info {
offer: "", // why is this reset to an empty string ??
description': "" // why is this reset to an empty string ??
completed: false
},
arrangement {
participant: "Tom",
bedroom': "Jones"
completed: false
}
}
}
我不知道为什么findByIdAndUpdate
重置/覆盖其他字段,而不是 $set!任何帮助表示赞赏。我需要在之后运行 .save()findByIdAndUpdate
吗?我有什么选择吗?
- 我运行一次,它会更新。
- 我运行第二个操作它更新但覆盖并重置上面的第一个
--- 这是我的测试代码
const assert = require('assert');
const Program = require('../models/program');
describe('Accomodation nested documents update', () => {
let program
let pid
beforeEach((done) => {
program = new Program({
name: "Sports",
description: "sddsdsdsdsdsd"
})
program.save().then((savedProgram) => {
pid = savedProgram.id;
done();
})
})
it('2. Update accomodation : INFO', (done) => {
Program.updateOne({ _id: pid },
{
$set: {
'accommodation.info.offer': "Yes",
'accommodation.info.description': "Something",
}
}
)
.then(() => Program.findById(pid))
.then((program) => {
assert(program.accommodation.info.offer === "Yes")
done();
})
});
it('3. Update accomodation : ARRANGEMENTS', (done) => {
Program.updateOne({ _id: pid },
{
$set: {
"accommodation.arrangement.participant": "Tom",
"accommodation.arrangement.bedroom": "Jones"
}
}
)
.then(() => Program.findById(pid))
.then((program) => {
assert(program.accommodation.arrangement.participant === "Tom");
assert(program.accommodation.info.offer === "Yes"); // This test fails
done();
})
});
});
该测试通过了第一次测试,第二次失败了
- program.accommodation.info.offer === "是"
解决方案
推荐阅读
- javascript - 谷歌浏览器不接受分页符
- azure - 使用 ADB2C 和共享的 Auth Cookie 在 Azure 应用服务上获取无限重定向循环
- powerbi - 如何在 Power BI 中为每个日历月生成日历表并使其从今天停止
- p5.js - 我想一次绘制一个数组 data[6] 的每个映射索引
- ravendb-studio - 如何在 RQL 中查询 $type 等属性路径表达式中的特殊字符,如 $
- javascript - Highcharter R 包:在进行拖动选择后防止图表上的缩放事件
- .net - 配置 Jenkins 以在 Linux 平台上运行 MSBuild
- ruby-on-rails - 使用 Bulma 将 Rails 部署到 Heroku
- r - 有没有一种整洁的方法来改变小标题中的单个细胞?
- angular - 如何使用 ag-grid 显示特定的无行覆盖?