<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<div class="container">
<div class="row">
<h2>Booking</h2>
</div>
</div>
const result = await db.orders.aggregate([
{
$lookup: {
from: "customers",
let: { customerId: "$customerId" }, // Define a variable to use in the pipeline
pipeline: [
{
$match: {
$expr: { $eq: ["$_id", "$$customerId"] } // Use the variable defined in $let
}
},
{
$match: { country: "USA" } // Additional filter based on country
}
],
as: "customerDetails"
}
},
{
$unwind: {
path: "$customerDetails",
preserveNullAndEmptyArrays: true // Preserve orders with no matching customer
}
}
]);
app.get("/getbooking", auth, async (req, res) => {
try {
let user_id = req.user.data._id;
console.log(user_id, "iddd");
// let result = await Booking.find({userId: new mongoose.Types.ObjectId(user_id)});
let result = await Booking.aggregate([
{
$match: { userId: new mongoose.Types.ObjectId(user_id) }, // Match bookings for the specific user
},
{
$lookup: {
from: "users", // Join with the "users" collection
foreignField: "_id", // User collection's _id field
localField: "userId", // Booking collection's userId field
as: "userDetails", // The resulting array containing user details
},
},
{
$unwind:{
path:"$userDetails",
preserveNullAndEmptyArrays: true
}, // Convert the userDetails array to a single object
},
{
$group: {
_id: "$userId", // Group by userId
user: { $first: "$userDetails" }, // Include user details
bookings: { $push: { _id: "$_id", userId: "$userId" , name: "$name" } }, // Group booking fields
},
},
{
$project:{
_id: 0,
user: '$user', // Include user details
booking:'$bookings',
bookingCount: {$size: '$bookings'}
}
}
]);
console.log(result,"resssss")
// successRes(res, {result, count:result.length}, 200, "Booking fetch Saved!!");
successRes(res, result, 200, "Booking fetch Saved!!");
} catch (error) {
errorRes(res, 500, "Internal Server Error!!");
}
});