How does PHP query for a yearly recurring schedule in MongoDB?

Source: Internet
Author: User
Tags repetition
The requirement is to query for a period of time, some of the schedule is repeated each year (month, day), how to get the schedule within a find?
The repetition means that the addition of the May 1 schedule of this year, I query next April-May schedule, still can find his
I hope I can give you a clue

Reply content:

The requirement is to query for a period of time, some of the schedule is repeated each year (month, day), how to get the schedule within a find?
The repetition means that the addition of the May 1 schedule of this year, I query next April-May schedule, still can find his
I hope I can give you a clue

If there is such a need, it should be stored in the data when the number of fields to express the "month" and "Day" information, which does not matter to PHP, but the design of MONGODB data structure.

For example, add a field:

{  "repeat_date": {    "month": 5,    "day": 1  }}

When you query, you can write:

// 找到所有月份为 5 的db.foo.find({"repeat_date.month": 5})

This repeat_date field is added only on the yearly recurring schedule and does not need to be added for non-recurring schedules.

If you need to identify all the schedules in a certain time period, including the yearly recurring schedule, you can query:

// 假设每个日程都有一个类型为 `ISODate` 的 `date` 字段表示日程发生在哪一天db.schedule.find({  $or: [    {      date: {        $gte: new Date("2014-03-01 00:00:00"),        $lt: new Date("2014-05-01 00:00:00")      },    },    {      "repeat_date.month": {        $gte: 3,        $lt: 5      }    }  ]})
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.