Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicating PostgreSQL's window functions like lag, lead, over

How do I change a PostgreSQL query into a mongodb bson call? I have the same use case listed at http://archives.postgresql.org/pgsql-general/2011-10/msg00157.php I would like to calculate the delta time between two log entries by using something like lag or lead. Is there anything similar in mongodb to Postgres' lag / lead syntax?

select 
  index, 
  starttime, 
  endtime, 
  starttime - lag(endtime) over(order by starttime asc) as delta 
from test

http://www.postgresql.org/docs/8.4/static/functions-window.html

I was looking at http://www.mongovue.com/2010/11/03/yet-another-mongodb-map-reduce-tutorial/ and it seems that map / reduce / finalize should do it. Map the id, start and end time, reduce does nothing, then do a inner join on its self (the double fors) during the finalize. I can almost, kind of, sort of, see it...

like image 355
user1195970 Avatar asked Nov 20 '25 18:11

user1195970


1 Answers

This is something you'll have to do in your application. Right now, mongoDB doesn't support anything like this.

like image 182
Derick Avatar answered Nov 23 '25 23:11

Derick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!