I want to make a complex query.
I'm building a cinema application. I want to make a validation check for a new show. it will check if the show time is in a range of another show that plays in the same theater. In order to know the end time of the range i need to add the number of minutes of the movie to the movie start time.
Important to note: The new show time is not yet stored in the DB. so it's in different timezone (let's say +2). so when we compare the dates we should take into effect the different timezones as well. I changed the time of the new show to utc so i guess it solves the issue.
Here is the code i tried running. obviously is not working. I don't know can i add the movie Duration to the movie start:
router.post("/", (req, res) => {
if (!req.body)
return res.sendStatus(400);
let show = new Show(req.body);
startDateTime= moment.utc(show.dateTime);
console.log('startDateTime in utc');
console.log(startDateTime);
Show.aggregate([
//next stage: add movies details
{
"$match": {
"dateTime": { $gte: new Date(startDateTime)},
"_TheaterId": show._TheaterId
}
},
{
$lookup:
{
from: 'movies',
localField: '_MovieId',
foreignField: '_id',
as: 'MovieDetails'
}
},
{
$unwind: "$MovieDetails"
},
{
"$match": {
{ $add: [ "$dateTime", $MovieDetails.durationInMin * 60000 ] }: { $lte: new Date(startDateTime)}
}
}
], function (err, result) {
if (err) {
//next(err);
return res.json(err);
} else {
res.json(result);
}
});
ANybody knows how can i add the minutes to the date field and then compare it to a different date?
Many thanks!
Here is a picture of the Db:
2条答案
按热度按时间d6kp6zgx1#
有力矩提供的加法功能。
因此假设您的 startDateTime 是一个时刻对象:
开始日期时间.add(20,'分钟');
yhived7q2#
You can add minutes to the date by using the moment() as follows.
To get the current time using moment() //current time
// To add the five minutes to the current date using moment().add()
// To subtract the five minutes from the current date using moment().subtract()
//To compare the date with other date using the moment().isBefore() and it will return Boolean
Sample program: