2017-02-15 115 views
0

我有一個集合名爲「listenedMessage」在我的MongoDB,它有一個名爲「timeReceived」 我如何找到基於「timeReceived」的字段,它是超過一年的所有記錄字段?查找所有記錄超過一年

+0

計算這正好是一歲的任何一種語言,您使用的日期,並把它傳遞在蒙戈, db.test.find({ 「timeReceived」:{$ LT:last_year_date}}) –

+0

@Puneet感謝名單了很多。我可以創建一個簡單的腳本了你的建議,而其真正的作品了。 –

回答

1

基於@Puneet辛格的評論,我來到這檢索具有「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