Last active
January 12, 2020 03:36
-
-
Save ruslanguns/ccded8982e24b28896e6f450cc82f3d0 to your computer and use it in GitHub Desktop.
Get stats from MongoDB using Aggregation Pepiline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pastMonth = new Date(); | |
pastMonth.setMonth(pastMonth.getMonth() - 1); // 1 month ago | |
var ultimasCargas = [ | |
{ $match: { createdAt: { $exists: true } } }, | |
{ $sort: { createdAt: -1 } }, | |
{ $limit: 5 }, | |
{ $project: { brand: 1, productId: 1, price: 1, status: 1, createdAt: 1 } } | |
]; | |
var ultimasVentas = [ | |
{ $match: { status: 'vendido' } }, | |
{ $match: { soldAt: { $exists: true } } }, | |
{ $sort: { soldAt: -1 } }, | |
{ $limit: 5 }, | |
{ $project: { brand: 1, productId: 1, price: 1, status: 1, createdAt: 1 } } | |
]; | |
var cargaMesAnterior = [ | |
{ | |
$redact: { | |
$cond: [ | |
{ $and: [ { $eq: [ { $month: "$createdAt" }, { $month: pastMonth } ] }, { $eq: [ { $year: "$createdAt" }, { $year: pastMonth } ] }] }, | |
"$$KEEP", | |
"$$PRUNE" | |
] | |
}, | |
}, | |
{ | |
$group: { | |
_id: null, | |
cantidad: { $sum: '$price' }, | |
unidades: { $sum: 1 }, | |
// datos: { $push: '$$CURRENT'} | |
} | |
}, | |
{ $project: { _id: 0, cantidad: 1, unidades: 1 } }, | |
]; | |
var cargaEsteMes = [ | |
{ | |
$redact: { | |
$cond: [ | |
{ $and: [ { $eq: [ { $month: "$createdAt" }, { $month: new Date() } ] }, { $eq: [ { $year: "$createdAt" }, { $year: new Date() } ] }] }, | |
"$$KEEP", | |
"$$PRUNE" | |
] | |
}, | |
}, | |
{ | |
$group: { | |
_id: null, | |
cantidad: { $sum: '$price' }, | |
unidades: { $sum: 1 }, | |
// datos: { $push: '$$CURRENT'} | |
} | |
}, | |
{ $project: { _id: 0, cantidad: 1, unidades: 1 } }, | |
]; | |
var cargadas = [ | |
{ | |
$group: { | |
_id: null, | |
cantidad: { $sum: '$price' }, | |
unidades: { $sum: 1 }, | |
// datos: { $push: '$$CURRENT'} | |
} | |
}, | |
{ $project: { _id: 0, cantidad: 1, unidades: 1 } }, | |
]; | |
var ventasMesAnterior = [ | |
{ | |
$redact: { | |
$cond: [ | |
{ $and: [ { $eq: [ { $month: "$soldAt" }, { $month: pastMonth } ] }, { $eq: [ { $year: "$soldAt" }, { $year: pastMonth } ] }] }, | |
"$$KEEP", | |
"$$PRUNE" | |
] | |
}, | |
}, | |
{ | |
$group: { | |
_id: null, | |
cantidad: { $sum: '$price' }, | |
unidades: { $sum: 1 }, | |
// datos: { $push: '$$CURRENT'} | |
} | |
}, | |
{ $project: { _id: 0, cantidad: 1, unidades: 1 } }, | |
]; | |
var ventasEsteMes = [ | |
{ $match: { status: 'vendido' } }, | |
{ | |
$redact: { | |
$cond: [ | |
{ $and: [ { $eq: [ { $month: "$soldAt" }, { $month: new Date() } ] }, { $eq: [ { $year: "$soldAt" }, { $year: new Date() } ] }] }, | |
"$$KEEP", | |
"$$PRUNE" | |
] | |
}, | |
}, | |
{ | |
$group: { | |
_id: null, | |
cantidad: { $sum: '$price' }, | |
unidades: { $sum: 1 }, | |
// datos: { $push: '$$CURRENT'} | |
} | |
}, | |
{ $project: { _id: 0, cantidad: 1, unidades: 1 } }, | |
]; | |
var vendidos = [ | |
{ $match: { status: 'vendido' } }, | |
{ | |
$group: { | |
_id: null, | |
cantidad: { $sum: '$price' }, | |
unidades: { $sum: 1 }, | |
// datos: { $push: '$$CURRENT'} | |
} | |
}, | |
{ $project: { _id: 0, cantidad: 1, unidades: 1 } }, | |
]; | |
var enVenta = [ | |
{ $match: { status: 'a la venta' } }, | |
{ | |
$group: { | |
_id: null, | |
cantidad: { $sum: '$price' }, | |
unidades: { $sum: 1 }, | |
// datos: { $push: '$$CURRENT'} | |
} | |
}, | |
{ $project: { _id: 0, cantidad: 1, unidades: 1 } }, | |
]; | |
var visitas = [ | |
{ | |
$project: { | |
settingsUsername: 'admin', | |
}, | |
}, | |
{ $limit: 1 }, | |
{ | |
$lookup: { | |
from: 'settings', | |
localField: 'settingsUsername', | |
foreignField: 'username', | |
as: 'settings', | |
} | |
}, | |
{ | |
$project: { | |
general: { $arrayElemAt: [ "$settings.totalViews", 0 ] }, | |
} | |
}, | |
]; | |
var query = { | |
$facet: { | |
ultimasCargas, | |
ultimasVentas, | |
cargaMesAnterior, | |
cargaEsteMes, | |
cargadas, | |
ventasMesAnterior, | |
ventasEsteMes, | |
vendidos, | |
enVenta, | |
visitas, | |
} | |
}; | |
var projectData = { | |
$project: { | |
visitas: { $arrayElemAt: [ "$visitas.general", 0 ] }, | |
enVenta: { $arrayElemAt: [ "$enVenta", 0 ] }, | |
carga: { | |
general: { $arrayElemAt: [ "$cargadas", 0 ] }, | |
cargaMesAnterior: { $arrayElemAt: [ "$cargaMesAnterior", 0 ] }, | |
cargaEsteMes: { $arrayElemAt: [ "$cargaEsteMes", 0 ] }, | |
ultimasCargas: "$ultimasCargas" | |
}, | |
ventas: { | |
general: { $arrayElemAt: [ "$vendidos", 0 ] }, | |
ventasMesAnterior: { $arrayElemAt: [ "$ventasMesAnterior", 0 ] }, | |
ventasEsteMes: { $arrayElemAt: [ "$ventasEsteMes", 0 ] }, | |
ultimasVentas: "$ultimasVentas" | |
} | |
}, | |
} | |
db.getCollection('bikes').aggregate([query, projectData]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
El resultado es: