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

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-10 04:55:10

问题


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...


回答1:


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




回答2:


You can rewrite some of the window functions as subqueries. See if that's possible in the aggregation framework. This subquery should after the filtering and grouping are done.

Couchbase is going to have the standard window functions. https://blog.couchbase.com/on-par-with-window-functions-in-n1ql/



来源:https://stackoverflow.com/questions/9185962/duplicating-postgresqls-window-functions-like-lag-lead-over

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!