0

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
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.