检索今天生日的文件

时间:2017-12-30 12:47:45

标签: mongodb mongodb-query aggregation-framework

我以下名为"成员":

的集合
{ 
    "_id" : ObjectId("5a477681bbe5f506e68d29b7"),
    "name" : "mario", 
    "surname" : "rossi", 
    "email" : "test@gmail.com", 
    "birth" : ISODate("1998-12-29T23:00:00Z"), 
    "fc" : null, "expires" : ISODate("2018-12-29T23:00:00.183Z")
},
{ 
    "_id" : ObjectId("5a477943bbe5f506e68d29b8"),
    "name" : "mario", 
    "surname" : "rossi", 
    "email" : "test@gmail.com", 
    "birth" : new Date, 
    "fc" : null, "expires" : ISODate("2018-12-29T23:00:00.570Z")
}

现在我想只返回今天生日那天的会员的姓名和电子邮件。你知道我可以使用什么查询吗?

谢谢

1 个答案:

答案 0 :(得分:0)

试试这个......

var currentDate = new Date(),
    date = currentDate.getDate(),
    month = currentDate.getMonth() + 1; // getMonth returns (0-11)

db.members.aggregate([
{
    $project:
    {
        name: 1,
        surname: 1,
        email: 1,
        date:
        {
            $dayOfMonth: '$birth'
        },
        month:
        {
            $month: '$birth'
        }
    }
},
{
    $match:
    {
        date: date, // Current date of the month
        month: month // Current month
    }
}]);
相关问题