I have a document in my collection:
{
"_id" : ObjectId("5b8aaaebf57de10e080c9151"),
"user_email" : "temp@temp.com",
"platforms_budget" : [
{
"_id" : ObjectId("5b8aaaebf57de10e080c9154"),
"platform_id" : "f_01",
"platform_name" : "Facebook"
},
{
"_id" : ObjectId("5b8aaaebf57de10e080c9153"),
"platform_id" : "i_01",
"platform_name" : "Instagram"
},
{
"_id" : ObjectId("5b8aaaebf57de10e080c9152"),
"platform_id" : "f_02",
"platform_name" : "Facebook_Adds"
}
],
"__v" : 0
}
I want to find specific user by "user_email" and get the length of the relevant "platform_budget" array. Which in this case suppose to be length=3.
My function is like this:
var BudgetSchema = require('../models/Budget');
router.post('/temp', async function (req, res) {
var length = await BudgetSchema.aggregate(
[{ $match: { user_email: "test@test.com" } }, { $unwind: "$platforms_budget" },
{ $project: { "platforms_budget.count": { $size: '$platforms_budget' } } }])
console.log(length);
})
When I try to console.log(length) I get an empty array.
I saw other answers on stackoverflow like this one, but I still can't understand what am I doing wrong or how to extract the size from the responce.
How do I get "platforms_budget" array size? Thank you.