【问题标题】:Expiring elements inside mongodb documentmongodb文档中的过期元素
【发布时间】:2013-05-09 07:01:37
【问题描述】:

注意:第一次使用 MongoDB。

技术:php mongodb 场景:

数据假设包含提供的程序。文档如下:

{
    LocationName: 'name of the location',
    LocationAddress: 'Main street., Popular City, PC',
    LocationManager: 'Mr. Manager',
    LocationPhone: '555 555 1212',
    Programs :
             [
                 {
                 ProgramName : 'Excellence Prgram',
                 ProgramStartDate : '12-05-2012',
                 ProgramEndDate : '3-15-2013'
                 },
                 {
                 ProgramName : 'Excellence Prgram for me',
                 ProgramStartDate : '1-05-2012',
                 ProgramEndDate : '2-15-2013'
                 },
                 {
                 ProgramName : 'Excellence Prgram for three',
                 ProgramStartDate : '1-05-2012',
                 ProgramEndDate : '4-15-2013'
                 }
             ]
}

问题:

  1. 要按开始和结束日期查询,集合查询是什么?
  2. 程序将在结束日期到期,并且经常将新程序添加到每个文档中。我认为在文档中保留过期的程序在某些时候会变得低效。如何归档过期的程序?

提前谢谢你。

【问题讨论】:

  • 显然这是一个很难解决的问题。

标签: mongodb mongodb-php


【解决方案1】:

请原谅我,但我讨厌给出这种类型的答案。我会彻底改变您存储文档的方式:

我会让文档看起来像这样:

 {
     _id : 1823098123908831809
     ProgramName : 'Excellence Prgram',
     ProgramStartDate : '12-05-2012',
     ProgramEndDate : '3-15-2013'
     Location : {
         LocationName: 'name of the location',
         LocationAddress: 'Main street., Popular City, PC',
         LocationManager: 'Mr. Manager',
         LocationPhone: '555 555 1212',
 }

您现在遇到的问题是,您可以查询具有 Active 程序的 Locations,但仅提取符合您的业务规则的程序会很棘手。

通过此重组,您可以为集合中日期符合您的业务规则的所有文档编写一个简单的查询。

位置更改细节确实变得更加复杂,但这只是一个小用例。您会经常查询活动程序。地点很少更换经理。

这也将允许您维护历史记录。如果您只更新程序相关运行的位置信息,您将记录 2013 年 5 月卓越程序运行在 Mains 街的旧位置与经理先生(即使它现在在 Front Street 举行,运行由经理夫人)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    • 2017-07-02
    相关资源
    最近更新 更多