问题
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 for
s) 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