Javascript 从数据库中获取2个计数器
我的表格中有以下文档结构:Javascript 从数据库中获取2个计数器,javascript,node.js,mongoose,Javascript,Node.js,Mongoose,我的表格中有以下文档结构: const mongoose = require('mongoose'); const logSchema = new mongoose.Schema({ eventName: { type: String, required: true, enum: ['connect', 'status', 'probability'], }, timestamp: { type: Date
const mongoose = require('mongoose');
const logSchema = new mongoose.Schema({
eventName: {
type: String,
required: true,
enum: ['connect', 'status', 'probability'],
},
timestamp: {
type: Date,
required: true,
}
}, {
timestamps: true
});
const Log = mongoose.model('Log', logSchema);
module.exports = Log;
我的意图是提取一个大小为7
(过去7天-过去一周)的数组。每个项目都是一个JSON,有两个主要字段:开始和结束的计数器。我想计算0方法1中的文档数
向$sum
添加条件:
weeklyLogs = await Log.aggregate([
{ $match: { timestamp: { $gte: moment.tz('Asia/Jerusalem').subtract(6, 'days').startOf('day').toDate() } } },
{
$group: {
_id: {
$dateFromParts: {
year: { $year: { date: "$timestamp", timezone: "Asia/Jerusalem" } },
month: { $month: { date: "$timestamp", timezone: "Asia/Jerusalem" } },
day: { $dayOfMonth: { date: "$timestamp", timezone: "Asia/Jerusalem" } },
timezone: "Asia/Jerusalem"
}
},
start: {
$sum: {
$cond: [ { $eq: ['$eventName', 'connect'] }, 1, 0]
}
},
end: {
$sum: {
$cond: [ { $ne: ['$eventName', 'connect'] }, 1, 0]
}
}
}
}
]);
方法2
您可以使用$facet
处理多个聚合管道,如下所示:
weeklyLogs = await Log.aggregate([
{ $match: { timestamp: { $gte: moment.tz('Asia/Jerusalem').subtract(6, 'days').startOf('day').toDate() } } },
{
$facet: {
start: [
{ $match: { eventName: 'connect' } },
{
$group: {
_id: {
$dateFromParts: {
year: { $year: { date: "$timestamp", timezone: "Asia/Jerusalem" } },
month: { $month: { date: "$timestamp", timezone: "Asia/Jerusalem" } },
day: { $dayOfMonth: { date: "$timestamp", timezone: "Asia/Jerusalem" } },
timezone: "Asia/Jerusalem"
}
},
documents: { $sum: 1 }
}
}
],
end: [
{ $match: { eventName: { $ne: 'connect' } } },
{
$group: {
_id: {
$dateFromParts: {
year: { $year: { date: "$timestamp", timezone: "Asia/Jerusalem" } },
month: { $month: { date: "$timestamp", timezone: "Asia/Jerusalem" } },
day: { $dayOfMonth: { date: "$timestamp", timezone: "Asia/Jerusalem" } },
timezone: "Asia/Jerusalem"
}
},
documents: { $sum: 1 }
}
}
]
}
}
]);
现在,\u id
字段显示在开始和结束中。有办法把它移到外面吗?所以json看起来像{u id:…,start:…,end:…}
?编辑:NVM,第一种方法是我需要的,谢谢。检查方法1,它应该有你需要的输出
weeklyLogs = await Log.aggregate([
{ $match: { timestamp: { $gte: moment.tz('Asia/Jerusalem').subtract(6, 'days').startOf('day').toDate() } } },
{
$facet: {
start: [
{ $match: { eventName: 'connect' } },
{
$group: {
_id: {
$dateFromParts: {
year: { $year: { date: "$timestamp", timezone: "Asia/Jerusalem" } },
month: { $month: { date: "$timestamp", timezone: "Asia/Jerusalem" } },
day: { $dayOfMonth: { date: "$timestamp", timezone: "Asia/Jerusalem" } },
timezone: "Asia/Jerusalem"
}
},
documents: { $sum: 1 }
}
}
],
end: [
{ $match: { eventName: { $ne: 'connect' } } },
{
$group: {
_id: {
$dateFromParts: {
year: { $year: { date: "$timestamp", timezone: "Asia/Jerusalem" } },
month: { $month: { date: "$timestamp", timezone: "Asia/Jerusalem" } },
day: { $dayOfMonth: { date: "$timestamp", timezone: "Asia/Jerusalem" } },
timezone: "Asia/Jerusalem"
}
},
documents: { $sum: 1 }
}
}
]
}
}
]);