查找超过一年的所有记录

时间:2017-02-15 06:32:36

标签: mongodb mongodb-query

我有一个名为" listenedMessage"在我的MongoDb中它有一个名为" timeReceived" 如何根据" timeReceived"找到所有超过一年的记录?场?

1 个答案:

答案 0 :(得分:1)

基于@Puneet Singh的评论,我找到了一个解决方案,该解决方案检索所有记录的“timeReceived”字段超过特定月份。

  function addMonths(date, months) {
    date.setMonth(date.getMonth() + months);
    return date;
  }

  db.listenedMessage.find({"timeReceived" : {$lt: addMonths(new Date(), -1)}}).count(); //this will give all the records having timeReceived field   older than a month from today
相关问题