Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7129139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:12:41+00:00 2026-05-28T11:12:41+00:00

consider the following document structures: Thread: – doc_type 1 – _id – subject (string)

  • 0

consider the following document structures:

Thread:

 - doc_type   1
 - _id        
 - subject    (string)

Posts:

 - doc_type   2
 - _id        
 - thread_id  (_id of Thread)
 - time       (milliseconds since 1970)
 - comment    (string)

I need the threads sorted by the last post on a thread, together with latest 5 posts.
I thought to avoid updating the thread document every time a new post is done in order to eliminate probability of conflicts in a distributed environment across db nodes. Besides, it will be working for the DB where the DB should be working for you.

For simplicity – lets’ just start with finding the latest post. The 5 posts can be gathered the same way.

Now, I’m not sure I’m on the right direction, however, looking here I found how to find the last post in a thread using a reduce function that uses a group-level to return thread subject taken from doc-type 1, and the last post document taken from doc-type 2.

BTW – opposed to the sample in the link, in my case a thread is always created with a first post, (so, for example, the creation date of a Thread will be the date of it’s first Post).

map:

function(doc){
  switch(doc.doc_type){
     case 1: emit([doc._id],doc); return;
     case 2: emit([doc.thread_id],doc); return;
  }
}

reduce:
on real world keys are more compound, so it must be used with appropriate group-level.
I also ignore here the case of re-reduce, just for simplicity’s sake.
You can find full picture here:

function(keys, vals, rr){
   var result = { subject: null, lastPost: null, count :0 };
   //I'll ignore the re-reduce case for simplicity
   vals.forEach(function(doc){
      switch(doc.doc_type){
         case 1: 
            result.subject = doc.subject; 
            return;
         case 2: 
            if (result.lastPost.time < doc.time) result.lastPost = doc; 
            result.count++;
            return;
      }
   });
   return result;
}

But how do I page it afterwards sorted by the latest-post date?
Is there a way to feed doc-ids from a result of a query as the filter criteria of another (preferably, using one round-trip)?

There is no limit to the number of posts in a thread, so I’m a little reluctant to relay on list function here, when the page-size can also vary, what will result in the last post not showing at all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:12:42+00:00Added an answer on May 28, 2026 at 11:12 am

    If you’re only after the last post or the last five posts, there’s a much simpler method. You can completely avoid the reducer, in fact.

    If you add the time as the second portion of the key, you can use a combination of endkey, descending, and limit to get the last N posts based on the thread_id.

    Here’s the MapReduce I wrote with some test data based on your schemas:

    function(doc) {
      if (doc.type) {
        if (doc.subject) {
          emit([doc._id, doc.time], doc.subject);
          emit([doc._id, 'Z'], doc.subject);
        } else {
          emit([doc.thread_id, doc.time], {_id: doc._id});
        }
      }
    }
    

    The strange output of the ‘Z’ key is to allow you to get the subject from the “bottom” of the list of items.

    The query parameters would look something like:

    ?endkey=["thread_id"]&descending=true&limit=6
    

    The limit should be N+1 where N is the number of posts you’d like back. In the results you’ll have the thread subject and _id objects (or whatever you’d like) from the post documents.

    The _id objects are output in this example so you can use it with include_docs=true if you want the full post. Toss in whatever other data from the post document you want (title, etc) to keep the overall index size low and use include_docs in those places where you need the full contents of the document. However, if you always need the full post document, output it in the emit as that will give you a faster response (though a larger index size on disk).

    Also, if you need a list of all threads sorted by last post as well as 5 posts per thread, you’d need to output keys like [time, thread_id, 'thread'] and [time, thread_id, 'post'] and use a _list to collect the posts “under” each thread document as the time sorting will cause threads and posts to be farther apart in the results. A _list function can then be used to combine/find them again. However, doing two requests may still be easier/lighter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following python class: class Event(Document): name = StringField() time = DateField() location
Consider the following snippet: $(document).bind('mousemove', function(e) { $('#someDiv').css({left: e.pageX+'px', top: e.pageY+'px'}); }); This should
Consider two web pages with the following in their body respectively: <body> <script> document.writeln('<textarea></textarea>')
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider following program: static void Main (string[] args) { int i; uint ui; i
Consider the following document <html> <body> This is some content <script type=text/javascript src=verySlowToRespond.js></script> This
Consider following XML document fragment: <Book> <Title>Example</Title> <Content> Some line </Content> <TOC> Again some
Consider the following code: $(document).click(function (event) { console.log(Ok); }); $(document).add($('p')).click(function onceHandler(event) { console.log('Clicked.'); });
Consider the following RTF document {\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fswiss\fprq2\fcharset0 Segoe UI;}{\f1\fswiss\fcharset0 Arial;}} {\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\f0\fs22 Sample Text\f1\fs20\par
Consider the following scanning procedure in a typical document handling webapp: The user scans

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.