英文:
I want to update array of objects which include datetime field in mongodb
问题
你的查询是用于更新MongoDB文档中的 start_time
和 end_time
字段,使它们增加指定数量的小时(在这种情况下,是一个小时)。以下是查询的翻译:
updateOne(
{ _id:id },
[{
$set: {
start_time: { $dateAdd: { startDate: "$start_time", unit: "minute", amount: hours}},
end_time: { $dateAdd: { startDate: "$end_time", unit: "minute", amount: hours } }
}
}]
)
这个查询将使 start_time
和 end_time
增加一个小时,正如你所描述的。希望这对你有帮助。
英文:
Hi I am new to mongodb trying to update date fields in array of objects.
Below I have mentioned my model and I have mentioned my query, it's working for start_time and end_time but not update array of objects but I need to update dateTime field which is in objects in time_slots array.
These are my mongodb sample documents:
db={
"teacher": [
{
"_id": "6434cf",
"start_time": ISODate("2023-03-22T07:00:25.787Z"),
"end_time": ISODate("2023-03-22T10:00:35.604Z"),
"duration": "15min",
"time_slots": [
{
"sl_no": 1,
"dateTime": ISODate("2023-03-22T06:30:25.787Z")
},
{
"sl_no": 2,
"dateTime": ISODate("2023-03-22T06:45:25.787Z")
}
]
},
{
"_id": "6434ctd",
"start_time": ISODate("2023-03-23T07:00:25.787Z"),
"end_time": ISODate("2023-03-23T10:00:35.604Z"),
"duration": "20min",
"time_slots": [
{
"sl_no": 1,
"dateTime": ISODate("2023-03-23T06:30:25.787Z")
},
{
"sl_no": 2,
"dateTime": ISODate("2023-03-23T06:50:25.787Z")
}
]
}
]
}
My query for updating start_time and end_time
updateOne(
{ _id:id },
[{
$set: {
start_time: { $dateAdd: { startDate: "$start_time", unit: "minute", amount: hours}},
end_time: { $dateAdd: { startDate: "$end_time", unit: "minute", amount: hours } }
}
}]
)
this is my desired output if a user wants to delay 1 hour
{
"_id": "6434cf" ,
"start_time":ISODate("2023-03-22T08:00:25.787Z"),
"end_time":ISODate("2023-03-22T11:00:35.604Z"),
"duration": "15min",
"time_slots": [
{
"sl_no": 1,
"dateTime": ISODate("2023-03-22T07:30:25.787Z")
},
{
"sl_no": 2 ,
"dateTime":ISODate("2023-03-22T07:45:25.787Z")
}
],
}
it should increase 1 hour in start_time, end_time and in dateTime filed in array.
答案1
得分: 2
One option is to use $map
:
db.teacher.updateOne(
{_id: id},
[
{$set: {
start_time: {$dateAdd: {startDate: "$start_time", unit: "minute", amount: hours}},
end_time: {$dateAdd: {startDate: "$end_time", unit: "minute", amount: hours}},
time_slots: {$map: {
input: "$time_slots",
in: {
sl_no: "$$this.sl_no",
dateTime: {$dateAdd: {
startDate: "$$this.dateTime", unit: "minute", amount: hours
}}
}
}}
}}
]
)
See how it works on the playground example
英文:
One option is to use $map
:
db.teacher.updateOne(
{_id: id},
[
{$set: {
start_time: {$dateAdd: {startDate: "$start_time", unit: "minute", amount: hours}},
end_time: {$dateAdd: {startDate: "$end_time", unit: "minute", amount: hours}},
time_slots: {$map: {
input: "$time_slots",
in: {
sl_no: "$$this.sl_no",
dateTime: {$dateAdd: {
startDate: "$$this.dateTime", unit: "minute", amount: hours
}}
}
}}
}}
]
)
See how it works on the playground example
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论