我想在MongoDB中更新包含日期时间字段的对象数组。

huangapple go评论75阅读模式
英文:

I want to update array of objects which include datetime field in mongodb

问题

你的查询是用于更新MongoDB文档中的 start_timeend_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_timeend_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

huangapple
  • 本文由 发表于 2023年4月11日 11:52:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定