2015-10-21 89 views
2

我希望能夠對所有匹配日期和月份但不是年份的記錄進行Mongo搜索。例如,給定機構像一個正式聲明如下:Mongo僅搜索月份和日期

{ 
    "lastName" : "a", 
    "date" : ISODate("1983-05-29T09:32:11.888Z"), 
}, { 
    "lastName" : "b", 
    "date" : ISODate("1959-05-29T09:32:11.888Z"), 
}, { 
    "lastName" : "c", 
    "date" : ISODate("1983-06-29T09:32:11.888Z"), 
} 

我傳遞某種形式的05/29

回答

1

你可以使用$ where條件

db.collection.find({$where : function() { return this.date.getMonth() == month && this.date.getDate() == day} }) 
時想了,並要返回b

您應該用您想要使用的實際值替換變量'month'和'day'。

1

請嘗試下面的聚合查詢。請參閱$dayOfMonth$month瞭解更多詳情。

db.test.aggregate(
{ "$project": {  
     "lastName": 1,  
     "month":{"$month":"$date"},  
     "day": { "$dayOfMonth":"$date" } 
}, 
{ "$match":{   
     "month": 5,  
     "day": 29 
    } 
}) 
相關問題