couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrew Koster <>
Subject How to query a view from within another view?
Date Wed, 25 Feb 2015 13:43:34 GMT
Dear all,

I have a CouchDB database with the following type of documents, representing
events that happen:

{ person: 1

  timestamp: 1

  event: { type: enter

           room: b }



{ person: 2

  timestamp: 5

  event: { type: leave

           room: b }


Now the problem that I want to solve is the following: I want to know the
total amount of time that every other person spent in the same room as
person 1, at the end of the day when all persons have left all rooms. Note
that any person can enter and leave many rooms at many different times. I
honestly don't know whether MapReduce is the best paradigm for this, or if I
should just export my data and write a separate script to figure this stuff
out (although this is probably not a feasible solution for our production

As a starting solution lets assume that all the data is sane, and thus
someone entering a room will also leave that room at a later time. However,
in a final solution this requirement will probably have to be relaxed,
because some events may be missing.

I have thought of a potential solution, but I have no idea whether this is
at all possible or how to do this in couchdb. Here is an outline.

1.    Create a view that emits the following format, for every person
entering a room event:

{ [room, person, timestamp], null }

2.    Create a view that emits { [room, timestamp], null} for every time
person 1 exits the room (could be for all people, but is unnecessary).

3.    Create a view that for each exiting a room event for any person except
person 1, does the following. In the mapping step:

.         Queries the first view to find the last timestamp when that person
entered the room.

.         Queries the first view to find all times before the exiting the
room event that person 1 entered that room

.         For each of those times, queries the second view to find all exit
times for that room, and for each interval checks what the overlap is.

.         Sum these overlaps together and emit as { person, time }

Reduce: for every person, sum all the times together.

However, this relies on me being able to figure out how to query a different
view from within a view. Does anybody know if that is possible, and if so,

Kind regards,

Andrew Koster


  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message