找不到猫鼬时获得结果

时间:2020-10-25 21:39:06

标签: node.js mongoose

我使用aggregate方法对数据库表中的数据进行分组。 这是我的模型:

const livenessLogSchema = new mongoose.Schema({
    eventName: {
        type: String,
        required: true,
        enum: ['connect', 'status', 'probability', 'liveness_error'],
    },
    timestamp: {
        type: Date,
        required: true,
    }
}, {
    timestamps: true
});

这是aggregate的使用方式:

const livenessConnectionsData = await LivenessLog.aggregate([
    {
        $match: {
            timestamp: {
                $gte: moment(new Date(startDate)).tz('Asia/Jerusalem').startOf('day').toDate(),
                $lte: moment(new Date(endDate)).tz('Asia/Jerusalem').endOf('day').toDate(),
            }
        }
    },
    {
        $group: {
            _id: {
                $dateToString: { format: '%d/%m/%Y', date: '$timestamp', timezone: 'Asia/Jerusalem' }
            },
            startConnection: {
                $sum: {
                    $cond: [{ $eq: ['$eventName', 'connect'] }, 1, 0]
                }
            },
            endConnection: {
                $sum: {
                    $cond: [{ $eq: ['$eventName', 'status'] }, 1, 0]
                }
            }
        }
    }
]);

基本上,在这种用法中,我得到的范围是startDateendDate(例如:09/24/202009/30/2020)2个计数器:startConnectionendConnection。 问题是:如果表中的特定日期在startDateendDate范围内,则根本没有文档({startDateendDate等于{{1} }),那么我不会在结果中得到它。

因此,如果日期范围是009/24/2020,并且该范围内包含文档的唯一日期是09/25/2020,那么我将得到结果:{{1 }}。

但是我确实希望将09/24/2020包含在结果中: [ { _id: '24/09/2020', start: 408, end: 308 } ]

如何?

0 个答案:

没有答案