检索今天生日的文件

问题描述 投票:1回答:1

我有以下名为“成员”的集合:

{ 
    "_id" : ObjectId("5a477681bbe5f506e68d29b7"),
    "name" : "mario", 
    "surname" : "rossi", 
    "email" : "[email protected]", 
    "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" : "[email protected]", 
    "birth" : new Date, 
    "fc" : null, "expires" : ISODate("2018-12-29T23:00:00.570Z")
}

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

谢谢

mongodb mongodb-query aggregation-framework
1个回答
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
    }
}]);
© www.soinside.com 2019 - 2024. All rights reserved.