app.get("/getbooking", auth, async (req, res) => {
try {
let user_id = req.user.data._id;
console.log(user_id, "iddd");
let result = await Booking.aggregate([
{
$match: { userId: new mongoose.Types.ObjectId(user_id) },
},
{
$lookup: {
from: "users",
foreignField: "_id",
localField: "userId",
as: "userDetails",
},
},
{
$unwind:{
path:"$userDetails",
preserveNullAndEmptyArrays: true
},
},
{
$group: {
_id: "$userId",
user: { $first: "$userDetails" },
bookings: { $push: { _id: "$_id", userId: "$userId" , name: "$name" } },
},
},
{
$project:{
_id: 0,
user: '$user',
booking:'$bookings',
bookingCount: {$size: '$bookings'}
}