Running the code below pops up a message telling me that 'pracownicy.length' is not a function. Where am I making a mistake and is my code sure to return the sum of salaries from each city? (The condition of the task is to use a function!)
db.getCollection("pracownicies").find({})
//ZADANIE_B -> Obliczyć sumę zarobków pracowników z podanego miasta.
function zadanie_b(miasto) {
pracownicy = db.pracownicies.aggregate([{
$group: {_id: (miasto),
sum_pensji:{$sum: "$Pensja"}
}
}]);
tablica = [];
for (var i = 0; i < pracownicy.length(); i++)
tablica.push(pracownicy[i]);
return tablica;
}
pracownicy = zadanie_b("Gdynia");
pracownicy.constructor.name
Output: Array TypeError: pracownicy.length is not a function : zadanie_b@(shell):10:23 @(shell):1:14 Object