Unable to get MongoDB Current Date


#1

Hello Guys,

I know this question is for mongodb, but I’ll try here just in case some people here already experienced this issue.

We have meteor app and we’re using mongodb as database. Now, since the VIEW feature is already available on mongodb we tried to use it to generate a report. However, we are having issue on comparison to our collection date field and to the current date, we cannot even retrieved current date field using mongodb aggregate under $project because we don’t know the right syntax to get the current date. Our goal is since it’s using view we would like to replace new Date() (which is the today’s date in below codes) to the current date method of mongodb.

"waiting_for_appointment" : {
        	$cond : {
        		if: { $eq: ["$current_status", "Waiting For Appointment"]},
        		then: {
        			$cond : {
        			 	 if: {$gt: [{$subtract: [ new Date(), "$statuses.waiting_for_appointment.from_date" ]}, 0]},
        			 	 then: {
        			 	 	$cond: {
        			 	 		if: {$gt: [{$subtract: [ "$statuses.waiting_for_appointment.to_date", "$statuses.waiting_for_appointment.from_date" ]}, 0]},
        			 	 		then: {$multiply : [{
        							$divide: [{$subtract: [ new Date(), "$statuses.waiting_for_appointment.from_date" ]}, {$subtract: [ "$statuses.waiting_for_appointment.to_date", "$statuses.waiting_for_appointment.from_date" ]}]
        							}, 100]
        			 	 		},
        			 	 		else: 0
        			 	 	}
        			 	 },
        			 	 else: 0
        			}
        		},
        		else: -1
        	}
        },

The query works correctly but when I edit again the VIEW query I found that it is converted to static date, the date when was the query is saved. This is what we got after saving the query and editing it back.

ISODate("2017-10-09T02:12:42.282+0000")

That’s why, the query will not be effective if the date is changed.

Just in case some people here already have a solution please share. :slight_smile:

Thanks!


#2

I haven’t tried this, but maybe $currentDate is what you want?
https://docs.mongodb.com/manual/reference/operator/update/currentDate/


#3

Thanks for the reply. I tried that already but that will only convert field to current date will not generate a date without field. Also it’s not a valid command for aggregate method of mongodb. :slight_smile: