I’m trying to get the total income of a specific seller. I already computed the total gross/income, but the problem here is instead of showing it only to the specific seller, it’s also being shown to others. (please ignore the spent)
router.get("/total/:id", async (req, res) => {
const { id } = req.params;
const date = new Date();
const lastMonth = new Date(date.setMonth(date.getMonth() - 1));
const previousMonth = new Date(new Date().setMonth(lastMonth.getMonth() - 1));
try {
const income = await Order.aggregate([
{$group: {_id: "6360d4d5bd860240e258c582", total: {$sum: "$amount"} }}
])
res.status(200).json(income);
} catch (err) {
res.status(500).json(err);
}
});
OrderSchema
const OrderSchema = new mongoose.Schema({
userId: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
products: [
{
productId:{
type: mongoose.Schema.Types.ObjectId, ref: 'Product'
},
quantity: {
type: Number,
default: 1,
},
sellerId: {
type: String
}
}
],
amount: {type: Number,required: true},
location:{type: Object, required:true},
time: {type: String, required: true},
status: {type:String, default: "pending"},
tax: {type: Number,}
}, {timestamps: true}
)
export default mongoose.model('Order', OrderSchema)
But the problem is, other accounts can also see the total income
2
Answers
like what @Charchit said.
You should first match the required documents, then group them, like this: