【问题标题】:Retrieve specific data by date with Angular and moment.js [closed]使用Angular和moment.js按日期检索特定数据[关闭]
【发布时间】:2017-10-26 07:45:17
【问题描述】:

我有这样的数据库结构(Firebase):

+ statistics
|-+ 2016
| |-+ 9
|   |-+ hours: 0
|-+ 2017
  |-+ 6
  | |-+ hours: 0
  |-+ 7
  |-+ 8
  |-+ 9 

父母,孩子(例如 9 月是 9 月) 创建一个列表来显示从一开始的所有小时的总和很容易年。例如从 2017 年开始。但是如果我想显示从 Steptember 2016 到 2017 年 9 月的所有小时数的总和呢?

我正在计划一个应用程序,这对这个应用程序很重要,一年从 9 月开始,到明年 9 月结束。所以我需要从2016->92017->9 的数据我该如何处理?

我正在使用 Ionic3 / Angular2、Firebase 和 moment.js 进行日期处理。

更具体:

我想得到这样的数据:Give me all 'hours'-values from September 2016 to September 2017 and build these to an array

【问题讨论】:

    标签: angular firebase-realtime-database momentjs ionic3 angular2-moment


    【解决方案1】:

    我假设您使用的是实时数据库;但是,我确信 Firestore 的设计/实现是相似的。

    您可能需要考虑这样的替代结构:

    statistics: {
      unix_ts_for_sept_2016: {
        unix_ts: unix_ts_for_sept_2016
        hours:
      },
      unix_ts_for_oct_2016: {
        unix_ts: unix_ts_for_oct_2016
        hours:
      },
      unix_ts_for_nov_2016: {
        unix_ts: unix_ts_for_nov_2016
        hours:
      }
      [,...]
    }
    

    时间戳作为键允许您获取/设置任何一个月,而时间戳作为子值允许您执行范围查询。

    const path_to_statistics = 'statistics/';
    const hours_field_name = 'hours';
    const ts_field_name = 'unix_ts';
    
    // Update the hours for a given month/yr in 'MM-DDDD' format
    var update_hours = function(mm_dash_yyyy, delta_hours) {
      const unix_ts = moment(mm_dash_yyyy, 'MM-YYYY').valueOf();
      const path = path_to_statistics + unix_ts + '/' + hours_field_name;
      const ref = firebase.database().ref(path);
      var deferred = $q.defer();
    
      ref.transaction(function(current) {
        if (current !== null) { return current + delta_hours; }
        return current;
      }, function(error) {
        if (error) { deferred.reject(error); }
        else { deferred.resolve(); }
      });
    
      return deferred.promise;
    };
    
    // Returns sum of hours in a given date range.
    var get_hours_in_range = function(start_mm_dash_yyyy, end_mm_dash_yyyy) {
      const start_ts = moment(start_mm_dash_yyyy, 'MM-YYYY').valueOf();
      const end_ts = moment(end_mm_dash_yyyy, 'MM-YYYY').valueOf();
      const ref = firebase.database().ref(path_to_statistics);
      const query = ref.orderByChild(ts_field_name).startAt(start_ts).endAt(end_ts);
      var deferred = $q.defer();
    
      query.once('value').then(function(snap) {
        var hours = 0;
        snap.forEach(function(child_snap) {
          hours += child_snap.val()[hours_field_name];
        });
        deferred.resolve(hours);
      }).catch(function(error) { deferred.reject(error); });
    
      return deferred.promise;
    };
    
    //usage
    update_hours('09-2016', 10).then(function() {
      console.log('updated hours.');
    }).catch(function(error) { console.error(error); })
    
    get_hours_in_range('09-2016', '10-2016').then(function(hours) {
      console.log(hours);
    }).catch(function(error) { console.error(error); });
    

    【讨论】:

    • 听起来很复杂.. 没有完全改变数据库结构的方法吗?如果没有 - 我会尝试你的方式。非常感谢! :)
    • 成功了,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多