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 5844873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:21:17+00:00 2026-05-22T12:21:17+00:00

I’ve been playing around with Couchbase Server and now just tried replicating my local

  • 0

I’ve been playing around with Couchbase Server and now just tried replicating my local db to Cloudant, but am getting conflicting results for my map/reduce function pair to build a set of unique tags with their associated projects…

// map.js
function(doc) {
  if (doc.tags) {
    for(var t in doc.tags) {
      emit(doc.tags[t], doc._id);
    }
  }
}

// reduce.js
function(key,values,rereduce) {
  if (!rereduce) {
    var res=[];
    for(var v in values) {
      res.push(values[v]);
    }
    return res;
  } else {
    return values.length;
  }
}

In Cloudbase server this returns JSON like:

{"rows":[
{"key":"3d","value":["project1","project3","project8","project10"]},
{"key":"agents","value":["project2"]},
{"key":"fabrication","value":["project3","project5"]}
]}

That’s exactly what I wanted & expected. However, the same query on the Cloudant replica, returns this:

{"rows":[
{"key":"3d","value":4},
{"key":"agents","value":1},
{"key":"fabrication","value":2}
]}

So it somehow only returns the length of the value array… Highly confusing & am grateful for any insights by some M&R ninjas… 😉

  • 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-22T12:21:18+00:00Added an answer on May 22, 2026 at 12:21 pm

    I prefer to reduce/re-reduce implicitly rather than depending on the rereduce parameter.

    function(doc) { // map
      if (doc.tags) {
        for(var t in doc.tags) {
          emit(doc.tags[t], {id:doc._id, tag:doc.tags[t]});
        }
      }
    }
    

    Then reduce checks whether it is accumulating document ids from the identical tag, or whether it is just counting different tags.

    function(keys, vals, rereduce) {
      var initial_tag = vals[0].tag;
    
      return vals.reduce(function(state, val) {
        if(initial_tag && val.tag === initial_tag) {
          // Accumulate ids which produced this tag.
          var ids = state.ids;
          if(!ids)
            ids = [ state.id ]; // Build initial list from the state's id.
          return { tag: val.tag, 
                 , ids: ids.concat([val.id])
                 };
        } else {
          var state_count = state.ids ? state.ids.length : state;
          var val_count   = val.ids   ? val.ids.length   : val;
          return state_count + val_count;
        } 
      })
    }
    

    (I didn’t test this code, but you get the idea. As long as the tag value is the same, it doesn’t matter whether it’s a reduce or rereduce. Once different tags start reducing together, it detects that because the tag value will change. So at that point just start accumulating.

    I have used this trick before, although IMO it’s rarely worth it.

    Also in your specific case, this is a dangerous reduce function. You are building a wide list to see all the docs that have a tag. CouchDB likes tall lists, not fat lists. If you want to see all the docs that have a tag, you could map them.

    for(var a = 0; a < doc.tags.length; a++) {
      emit(doc.tags[a], doc._id);
    }
    

    Now you can query /db/_design/app/_view/docs_by_tag?key="3d" and you should get

    {"total_rows":287,"offset":30,"rows":[
    {"id":"project1","key":"3d","value":"project1"}
    {"id":"project3","key":"3d","value":"project3"}
    {"id":"project8","key":"3d","value":"project8"}
    {"id":"project10","key":"3d","value":"project10"}
    ]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.