Node.js 选择当月有生日的所有用户

Node.js 选择当月有生日的所有用户,node.js,mongodb,mongoose,mongodb-query,aggregation-framework,Node.js,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,我刚刚开始学习NodeJs+MongoDB(Mongoose)。 查询有问题 我需要选择所有在当月过生日的用户 用户模式: const UserSchema = new mongoose.Schema({ email: { type: String, unique: true, required: true, index: true }, password: { type: String,

我刚刚开始学习NodeJs+MongoDB(Mongoose)。 查询有问题

我需要选择所有在当月过生日的用户


用户模式:

const UserSchema = new mongoose.Schema({
    email: {
        type: String,
        unique: true,
        required: true,
        index: true
    },
    password: {
        type: String,
        required: true
    },
    firstName: {
        type: String,
        required: true
    },
    lastName: {
        type: String,
        required: true
    },
    phone: {
        type: String,
        required: true
    },
    birthday: {
        type: Date,
        required: true
    },
    photo: {
        type: String,
        required: false
    },
    updated: {
        type: Date,
        default: Date.now
    }
});
{ 
    "__v" : NumberInt(0), 
    "_id" : ObjectId("589b26ab4490e29ab5bdc17c"), 
    "birthday" : ISODate("1982-08-17T00:00:00.000+0000"), 
    "email" : "test@gmail.com", 
    "firstName" : "John", 
    "lastName" : "Smith", 
    "password" : "$2a$10$/NvuIGgAYbFIFMMFW1RbBuRGvIFa2bOUQGMrCPRWV7BJtrU71PF6W", 
    "phone" : "1234567890", 
    "photo" : "photo-1486565456205.jpg", 
    "updated" : ISODate("2017-02-08T14:09:47.215+0000")
}
收集(用户)文档示例:

const UserSchema = new mongoose.Schema({
    email: {
        type: String,
        unique: true,
        required: true,
        index: true
    },
    password: {
        type: String,
        required: true
    },
    firstName: {
        type: String,
        required: true
    },
    lastName: {
        type: String,
        required: true
    },
    phone: {
        type: String,
        required: true
    },
    birthday: {
        type: Date,
        required: true
    },
    photo: {
        type: String,
        required: false
    },
    updated: {
        type: Date,
        default: Date.now
    }
});
{ 
    "__v" : NumberInt(0), 
    "_id" : ObjectId("589b26ab4490e29ab5bdc17c"), 
    "birthday" : ISODate("1982-08-17T00:00:00.000+0000"), 
    "email" : "test@gmail.com", 
    "firstName" : "John", 
    "lastName" : "Smith", 
    "password" : "$2a$10$/NvuIGgAYbFIFMMFW1RbBuRGvIFa2bOUQGMrCPRWV7BJtrU71PF6W", 
    "phone" : "1234567890", 
    "photo" : "photo-1486565456205.jpg", 
    "updated" : ISODate("2017-02-08T14:09:47.215+0000")
}

如果您有输入日期,即fromDate和toDate,则简单查询的结果是:

db.collection.find({"birthday":{$gte: fromDate, $lte: toDate}});

要获取当月所有过生日的用户的列表,您需要运行聚合操作,该操作使用管道过滤文档,并借助操作员进行编辑。考虑执行以下管道:

User.aggregate([
    {
        "$redact": {
            "$cond": [
                {
                    "$eq": [
                        { "$month": "$birthday" },
                        { "$month": new Date() }
                    ]
                }
            ],
            "$$KEEP",
            "$$PRUNE"
        }
    }
]).exec(function(err, docs){
    if (err) throw err;
    console.log(docs);
});

上面的表达式

"$cond": [
    {
        "$eq": [
            { "$month": "$birthday" },
            { "$month": new Date() }
        ]
    }
],
本质上表示条件语句

if (birthday.getMonth() === (new Date()).getMonth()) {
    "$$KEEP" // keep the document in the pipeline
} else {
    "$$PRUNE" // prune/discard the document from the output
}

管道将根据返回的系统变量返回所有符合条件的文档,否则将丢弃重复的文档。检查这个问题:这永远不会起作用,因为你只在一年内过滤。