Thank you so much, this solution worked. I put your code at the start of my code and removed the .toArray of my result… Here is the full code if anyone ever have the same issue.
import { Meteor } from "meteor/meteor";
import { Promise } from 'meteor/promise'
import _ from "lodash";
Mongo.Collection.prototype.aggregate = function(pipelines, options = {}) {
return Promise.await(this.rawCollection().aggregate(pipelines).toArray());
};
Meteor.methods({
examAuditForDoctorsInvoiceReport(startDate, endDate) {
const header = "[method] [examAuditForDoctorsInvoiceReport]";
// console.log(header, 'starting', requestedTenantIds, startDate, endDate)
if (!(startDate && endDate)) {
// console.log(header, 'startDate or endDate not provided -> skipping')
return [];
}
const userId = this.userId;
if (!userId) {
// console.log(header, 'user not logged it -> skipping')
return [];
}
const isGlobalAdmin = hasPermission(userId, "global:admin");
if (!isGlobalAdmin) return [];
const user = Meteor.users.findOne(userId);
if (!user) {
console.error(header, `failed to load user record for id ${userId} -> skipping`);
return [];
}
let query = {
startDate: { $gte: startDate, $lte: endDate },
status: { $in: ["Concluído", "Revisão"] },
};
let fields = {
userId: 1,
status: 1,
_id: 1,
startDate: 1,
examId: 1,
"exam.tenantId": 1,
"exam.type": 1,
"exam.status": 1,
"exam.doctorId": 1,
"exam.reviewerId": 1,
"exam.patientName": 1,
"exam.requesterName": 1,
"exam.createdAt": 1,
"exam.healthPlan": 1,
"exam.doctorName": "$exam.doctor.profile.fullName",
"exam.reviewerName": "$exam.reviewer.profile.fullName",
};
let pipeline = [
{ $match: query },
{
$lookup: {
from: "Exam",
localField: "examId",
foreignField: "_id",
as: "exam",
},
},
{ $unwind: "$exam" },
// { $match: { "exam.tenantId": { $in: authorizedTenantIdList } } },
{
$lookup: {
from: "users",
localField: "exam.doctorId",
foreignField: "_id",
as: "doctor",
},
},
{ $unwind: "$doctor" },
{
$lookup: {
from: "users",
localField: "exam.doctorId",
foreignField: "_id",
as: "exam.doctor",
},
},
{ $unwind: "$exam.doctor" },
{
$lookup: {
from: "users",
localField: "exam.reviewerId",
foreignField: "_id",
as: "exam.reviewer",
},
},
// { $unwind: "$exam.reviewer" },
{ $project: fields },
];
//console.log(header, "pipeline", pipeline);
let result = Exam_Audit.aggregate(pipeline, { cursor: {} }) || [];
return result;
},
});