I am trying to paginate an aggregation query using mongoose-aggregate-paginate
The field bookings is an array of ObjectId’s of collection “bookings”
BookingModel: model("booking", bookingSchema)
The model :
const tourGroupSchema = new Schema({
product: String,
date: String,
time: String,
bookings: [{ type: Schema.Types.ObjectId, ref: "booking" }], //need to be populated
task: String,
task_id: String,
notes: String,
notes_list: { type: Array, default: [] }, // -
guide_id: String,
guide_uds_id: String,
guide_confirmation: String,
guide_details: String,
guides_asked: Array,
guide_email_sent: Boolean,
vehicle_id: String,
index: {
type: Number,
default: 1,
},
});
tourGroupSchema.plugin(mongoosePaginate);
tourGroupSchema.plugin(mongooseAggregatePaginate);
TourGroupModel: model("tour_group", tourGroupSchema),
My pipeline :
const pipeline = [];
// Stage 1: Match documents based on product_ids
pipeline.push({
$match: {
product: { $in: product_ids },
},
});
//WHAT I HAVE TRIED
pipeline.push({
$lookup: {
from: "bookings",
localField: "bookings",
foreignField: "_id",
as: "bookings",
},
});
const aggregation = await TourGroupModel.aggregate(pipeline);
const data = await TourGroupModel.aggregatePaginate(aggregation, {
page: page,
limit: 25,
sort: { date: 1, time: 1, index: 1 },
});
result :
{
"docs": [
{
"_id": "64cc8fa5fe9011604f2dca21",
"product": "63958eaedaf242d73c7b9719",
"date": "2022-04-08",
"time": "15:00",
"bookings": [
"641c46c8e0edc61fec97a830",
"641c46c9e0edc61fec97a83a",
"641c46c8e0edc61fec97a835"
],
"index": 1,
"__v": 2,
"notes_list": []
},
...rest
As you can see the bookings array of each TourGroup dont come with populated documents but still their ids instead, any ideas why ?