6

これを行う方法を理解しようとしています。基本的に、提出物の時間/日/月/年で並べ替えたいです。

それぞれsubmissioncreated、Mongoose Date オブジェクトを の形式で含むフィールドがあります"created" : ISODate("2013-03-11T01:49:09.421Z")。find() 条件でこれと比較する必要がありますか?

これが私の現在のクエリです(FWIWのページネーションのためにカウントでラップしているので、その部分は無視してください):

  getSubmissionCount({}, function(count) {

  // Sort by the range
    switch (range) {
      case 'today':
        range = now.getTime();
      case 'week':
        range = now.getTime() - 7;
      case 'month':
        range = now.getTime() - 31; // TODO: make this find the current month and # of   days in it
      case 'year':
        range = now.getTime() - 365;
      case 'default':
        range = now.getTime();
    }

    Submission.find({
      }).skip(skip)
         .sort('score', 'descending')
         .sort('created', 'descending')
         .limit(limit)
         .execFind(function(err, submissions) {
            if (err) {
          callback(err);
        }

        if (submissions) {
          callback(null, submissions, count);
        }
    });
  });

誰かがこれを理解するのを手伝ってくれますか? その現在のコードでは、時間範囲に関係なくすべての提出物が表示されるだけなので、明らかに何かを適切に行っていません

4

1 に答える 1

17

$ltMongoDBで (Less than) および$gt(Greater Than) 演算子を探していると思います。上記の演算子を使用することで、時間に応じて結果を照会できます。

以下に可能な解決策を追加しています。

var d = new Date(),
hour = d.getHours(),
min = d.getMinutes(),
month = d.getMonth(),
year = d.getFullYear(),
sec = d.getSeconds(),
day = d.getDate();


Submission.find({
  /* First Case: Hour */
  created: { $lt: new Date(), $gt: new Date(year+','+month+','+day+','+hour+','+min+','+sec) } // Get results from start of current hour to current time.
  /* Second Case: Day */
  created: { $lt: new Date(), $gt: new Date(year+','+month+','+day) } // Get results from start of current day to current time.
  /* Third Case: Month */
  created: { $lt: new Date(), $gt: new Date(year+','+month) } // Get results from start of current month to current time.
  /* Fourth Case: Year */
  created: { $lt: new Date(), $gt: new Date(year) } // Get results from start of current year to current time.
})
于 2013-06-09T16:31:57.040 に答える