Chainmap / shrink in couchDB

In couchDB, I have a bunch of items like the following (simplified like sake for example):

{_id: 1, date: "Jul 1", user: "user1"}
{_id: 2, date: "Jul 2", user: "user1"}
{_id: 3, date: "Jul 3", user: "user2"}
...etc...

      

I would like to get a list of "most recent activities" sorted by date, without duplicate custom _ids. I can create a view with results like this:

{key: "July 3", _id: 3, user: "user2"}
{key: "July 2", _id: 2, user: "user1"}
{key: "July 1", _id: 1, user: "user1"}

      

but this contains duplicate entries for the same user. Or I can create a view that displays {key: user, value: date} and boils down to

{key: "user1", mostRecentDate: "July 2"}
{key: "user2", mostRecentDate: "July 3"}

      

but this is not sorted by "last".

I know the obvious solution is that the reduction of the results of another view is not supported. BigCouch does support anchored maps / shortcuts but seems to be deprecated / not supported (latest 2012).

This seems to be a fairly common problem - what are some of the existing solutions (besides "switch databases")?

+3


source to share


1 answer


Here is a general idea of ​​how you can convert a chain using couchdb 1.xxx. What we want is the ability to transfer the results of one card / reduce to another.

  • Subscribe to the _changes feed filtered in the view. This will give you a list of documents that will actually be selected by the map function.

  • Next, we need to call the view function for these filtered documents. This is simple because we can pass a list of keys to represent, so we just pass in the keys and get the desired subset of the result.

  • Then we push that result either in a separate database or in the same one. We can use bulk inserts to make inserts faster. If you are using a separate database, you can reuse _id's

    from view results to make bulk updates much easier.

  • In this database, we define another view that sorts our results based on value.

    {key: "user1", mostRecentDate: "July 2"} {key: "user2", mostRecentDate: "July 3"}

since you have already reached this step, all you need to do is create a view in mostRecentDate

the second database and you will get user activity sorted by date.



Hope you are using a dummy. One that returns null and is only used for group=true

.

using the list function in step 4 you can make your life easier. Since bulk updates require a list of documents to be on a form {"docs":[....]}

, you can easily get it in one go using the list function.

+1


source







All Articles