In my case I only want get a number to use it. This is my schema:
const personaSchema = mongoose.Schema({
nombre:String,
direccion: String,
dineroRecibido: Number,
fecha: String
},{versionKey: false})
this is my aggregate function:
const mostrarSumaDinerosPorDia = async (fecha) =>{
const sumaDineroHoy = await PersonaModel.aggregate(
[
{$match:{fecha:fecha}},
{$group:{_id:'$fecha' ,total:{$sum:'$dineroRecibido'}}}
]
)
console.log(sumaDineroHoy)
}
and this is my answer console:
[ { _id: '24 de diciembre del 2021', total: 10000 } ]
I only want get 10000, no more.
Add this to your aggregation pipeline { $project: { "_id": 0 } }
More info on $project here: $project MongoDB