【发布时间】:2021-10-17 20:28:52
【问题描述】:
我正在尝试设置嵌套数组对象的值 架构:
courses: [
{
days: [
{
courseDate: {
type: String,
},
attendance: {
type: Boolean,
},
reason: {
type: String,
},
},
],
courseId: {
type: String,
},
name: {
type: String,
},
startTime: {
type: String,
},
endTime: {
type: String,
},
},
],
这是我的尝试:
await Student.findOneAndUpdate(
{ "courses.days._id": "6117b0c45345db20f0dc3336" },
{ $set: { "courses.$.days.$.attendance": true } },
{ new: true }
);
await Student.findOneAndUpdate(
{ "courses.days._id": req.params.dayId },
{ "courses.$[courseIndex].days.$[dayIndex].attendance": true },
{
arrayFilters: [
{
courseIndex: req.params.courseId,
},
{
dayIndex: req.params.dayId,
},
],
}
);
这是文件:
"courses" : [
{
"_id" : ObjectId("6117b0c45345db20f0dc3330"),
"courseId" : "61155838b1fff211dd9a8765",
"name" : "succ",
"startTime" : "20:20",
"endTime" : "23:20",
"days" : [
{
"_id" : ObjectId("6117b0c45345db20f0dc3331"),
"courseDate" : "Wed Aug 04 2021 00:00:00 GMT+0300 (Israel Daylight Time)",
"attendance" : false,
"reason" : ""
},
{
"_id" : ObjectId("6117b0c45345db20f0dc3332"),
"courseDate" : "Wed Aug 11 2021 00:00:00 GMT+0300 (Israel Daylight Time)",
"attendance" : false,
"reason" : ""
},
{
"_id" : ObjectId("6117b0c45345db20f0dc3333"),
"courseDate" : "Wed Aug 18 2021 00:00:00 GMT+0300 (Israel Daylight Time)",
"attendance" : false,
"reason" : ""
},
{
"_id" : ObjectId("6117b0c45345db20f0dc3334"),
"courseDate" : "Wed Aug 25 2021 00:00:00 GMT+0300 (Israel Daylight Time)",
"attendance" : false,
"reason" : ""
}
]
},
{
"_id" : ObjectId("6117b0c45345db20f0dc3335"),
"courseId" : "61155838b1fff211dd9a8765",
"name" : "test",
"startTime" : "13:40",
"endTime" : "15:40",
"days" : [
{
"_id" : ObjectId("6117b0c45345db20f0dc3336"),
"courseDate" : "Thu Aug 05 2021 00:00:00 GMT+0300 (Israel Daylight Time)",
"attendance" : false,
"reason" : ""
},
{
"_id" : ObjectId("6117b0c45345db20f0dc3337"),
"courseDate" : "Thu Aug 12 2021 00:00:00 GMT+0300 (Israel Daylight Time)",
"attendance" : false,
"reason" : ""
},
{
"_id" : ObjectId("6117b0c45345db20f0dc3338"),
"courseDate" : "Thu Aug 19 2021 00:00:00 GMT+0300 (Israel Daylight Time)",
"attendance" : true,
"reason" : ""
},
{
"_id" : ObjectId("6117b0c45345db20f0dc3339"),
"courseDate" : "Thu Aug 26 2021 00:00:00 GMT+0300 (Israel Daylight Time)",
"attendance" : false,
"reason" : ""
}
]
}
],
第一个是抛出错误:
Too many positional (i.e. '$') elements found in path 'courses.$.days.$.attendance'
第二个不起作用...我真的不明白第二种方法的问题在哪里。
它找到了正确的文档,但它没有更新任何东西。
需要注意的是,每个数组的位置都是动态的,所以第二种方法也很好。
【问题讨论】:
-
确保对
req.params变量进行类型转换。默认情况下,它们是字符串类型。 -
还是不行:(
-
类型转换
"courses.days._id": ObjectId(req.params.dayId)。看我的回答 -
这能回答你的问题吗? arrayFilters in mongodb