Question

How we can perform the below given MySQL query in MongoDB preferably in MongoDB-4.0:

SELECT *,ABS(TIME_TO_SEC(TIMEDIFF(now(), datefield))/3600) as hours,
(field1*field2*field2+field3*(1/(ABS(TIME_TO_SEC(TIMEDIFF(now(), datefield))/3600)))) 
as time FROM temp_table
Was it helpful?

Solution

The equivalent MongoDB Query is

 db.temp_table.aggregate([{$project:{field1:1,field2:1,hours:{$abs:{$divide:[{$subtract:[ISODate(),"$datefield"]},3600000]}},time:{$sum:[{$multiply:["$field1","$field2","$field2"]},{$multiply:"$field3",{$divide:[1,{$abs:{$divide:[{$subtract:[ISODate(),"$datefield"]},3600000]}}]}]}]}}}])

Fields to be shown can be added as per the requirement.

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top