mongodb聚合:如何将分钟添加到日期字段,然后进行比较?

mcvgt66p  于 2022-12-12  发布在  Go
关注(0)|答案(2)|浏览(172)

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:

d6kp6zgx

d6kp6zgx1#

有力矩提供的加法功能。
因此假设您的 startDateTime 是一个时刻对象

开始日期时间.add(20,'分钟');

yhived7q

yhived7q2#

You can add minutes to the date by using the moment() as follows.

// To add the five minutes  to the current date
var moment = require('moment'); // require

To get the current time using moment() //current time

datenow = moment()

// To add the five minutes to the current date using moment().add()

datePulsFivemin = moment().add(5, 'm')//Add five minutes to the current work

// To subtract the five minutes from the current date using moment().subtract()

dateSubractFivemin = moment().subtract(5, 'm') //Add a .format() if need of UTC format

//To compare the date with other date using the moment().isBefore() and it will return Boolean

//Syntax
//sourcedate.isBefore(targetdate) 
moment().isBefore(datePulsFivemin)

Sample program:

var moment = require('moment'); // require

// Current date
datenow = moment()

//Add five minutes to the current date
datePulsFivemin = moment().add(5, 'm')

//subtract five minutes from the current date
dateSubractFivemin = moment().subtract(5, 'm') //Add a .format() if need of UTC format

console.log(moment()) //Moment<2022-12-10T19:36:09+05:30>
console.log(datePulsFivemin.format()) //2022-12-10T19:41:09+05:30
console.log(dateSubractFivemin.format()) //2022-12-10T19:31:09+05:30
// TO compare the date
console.log(moment().isBefore(datePulsFivemin)) //true
console.log(moment().isBefore(dateSubractFivemin)) //false

相关问题