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

  • SEARCH
  • Home
  • 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 7976881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:02:01+00:00 2026-06-04T09:02:01+00:00

I’m trying to implement a basic way of displaying comments in the way that

  • 0

I’m trying to implement a basic way of displaying comments in the way that Hacker News provides, using CouchDB. Not only ordered hierarchically, but also, each level of the tree should be ordered by a “points” variable.

The idea is that I want a view to return it in the order I except, and not make many Ajax calls for example, to retrieve them and make them look like they’re ordered correctly.

This is what I got so far:

  • Each document is a “comment”.
  • Each comment has a property path which is an ordered list containing all its parents.

So for example, imagine I have 4 comments (with _id 1, 2, 3 and 4). Comment 2 is children of 1, comment 3 is children of 2, and comment 4 is also children of 1. This is what the data would look like:

{ _id: 1, path: ["1"] },
{ _id: 2, path: ["1", "2"] },
{ _id: 3, path: ["1", "2", "3"] }
{ _id: 4, path: ["1", "4"] }

This works quite well for the hierarchy. A simple view will already return things ordered the way I want it.

The issue comes when I want to order each “level” of the tree independently. So for example documents 2 and 4 belong to the same branch, but are ordered, on that level, by their ID. Instead I want them ordered based on a “points” variable that I want to add to the path – but can’t seem to understand where I could be adding this variable for it to work the way I want it.

Is there a way to do this? Consider that the “points” variable will change in time.

  • 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-06-04T09:02:02+00:00Added an answer on June 4, 2026 at 9:02 am

    Because each level needs to be sorted recursively by score, Couch needs to know the score of each parent to make this work the way you want it to.

    Taking your example with the following scores (1: 10, 2: 10, 3: 10, 4: 20)

    In this case you’d want the ordering to come out like the following:

    .1
    .1.4
    .1.2
    .1.2.3
    

    Your document needs a scores array like this:

    { _id: 1, path: [1], scores: [10] },
    { _id: 2, path: [1, 2], scores: [10,10] },
    { _id: 3, path: [1, 2, 3], scores: [10,10,10] },
    { _id: 4, path: [1, 4], scores: [10,20] }
    

    Then you’ll use the following sort key in your view.

    emit([doc.scores, doc.path], doc)
    

    The path gets used as a tiebreaker because there will be cases where sibling comments have the exact same score. Without the tiebreaker, their descendants could lose their grouping (by chain of ancestry).

    Note: This approach will return scores from low-to-high, whereas you probably want scores (high to low) and path/tiebreaker(low to high). So a workaround for this would be to populate the scores array with the inverse of each score like this:

    { _id: 1, path: [1], scores: [0.1] },
    { _id: 2, path: [1, 2], scores: [0.1,0.1] },
    { _id: 3, path: [1, 2, 3], scores: [0.1,0.1,0.1] },
    { _id: 4, path: [1, 4], scores: [0.1,0.2] }
    

    and then use descending=true when you request the view.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.