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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:00:53+00:00 2026-05-16T11:00:53+00:00

Lets say I have two types of documents stored in my CouchDB database. First

  • 0

Lets say I have two types of documents stored in my CouchDB database. First is with property type set to contact and second to phone. Contact type document have another property called name. Phone type have properties number and contact_id so that it can reference to contact person. This is trivial one to many scenario where one contact can have N phone numbers (I know that they can be embedded in single contact document, but I need to demonstrate one to many relationship with different documents).

Raw example data with Scott having 2 phone numbers and Matt having 1 number:

{_id: "fc93f785e6bd8c44f14468828b001109", _rev: "1-fdc8d121351b0f5c6d7e288399c7a5b6", type: "phone", number: "123456", contact_id: "fc93f785e6bd8c44f14468828b00099f"}
{_id: "fc93f785e6bd8c44f14468828b000f6a", _rev: "1-b2dd90295693dc395019deec7cbf89c7", type: "phone", number: "465789", contact_id: "fc93f785e6bd8c44f14468828b00099f"}
{_id: "fc93f785e6bd8c44f14468828b00099f", _rev: "1-bd643a6b0e90c997a42d8c04c5c06af6", type: "contact", name: "Scott"}
{_id: "16309fcd03475b9a2924c61d690018e3", _rev: "1-723b7c999111b116c353a4fdab11ddc0", type: "contact", name: "Matt"}
{_id: "16309fcd03475b9a2924c61d69000aef", _rev: "3-67193f1bfa8ed21c68e3d35847e9060a", type: "phone", number: "789456", contact_id: "16309fcd03475b9a2924c61d690018e3"}

Map function:

function(doc) {
  if (doc.type == "contact") {
    emit([doc._id, 1], doc);
  } else if (doc.type == "phone") {
    emit([doc.contact_id, 0], doc);
  }
}

Reduce function:

function(keys, values) {
  var output = {};

  for(var elem in values) {
    if(values[elem].type == "contact") {
      output = {
        "ID": values[elem]._id,
        "Name": values[elem].name,
        "Type": values[elem].type,
        "Phones": []
      };
    } else if (values[elem].type == "phone") {
      output.Phones.push({ 
        "Number": values[elem].number, 
        "Type": values[elem].type 
      });
    }
  }

  return output;
}

group_level is set to 1 because of keys in Map function. Now I can get my contacts with included phones for example like this:

http://localhost:5984/testdb2/_design/testview/_view/tv1?group_level=1

Or search for some contact with startkey and endkey like this:

http://localhost:5984/testdb2/_design/testview/_view/tv1?group_level=1&startkey=[%22fc93f785e6bd8c44f14468828b00099f%22]&endkey=[%22fc93f785e6bd8c44f14468828b00099f%22,{}]

Results look exactly how I want – contacts will have embedded phones according to one to many relationship. And here goes the question: Is this the right way of how to use MapReduce functions in CouchDB? Are there any notable performance issues when using this approach?

  • 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-16T11:00:53+00:00Added an answer on May 16, 2026 at 11:00 am

    Generally speaking you use less disk space if you do not emit(...,doc).

    You may want to reconsider having a reduce function at all. It’s really not necessary to get at the data you need. For example, something along the lines of the following may use less disk space and perform better if you have a huge number of records.

    Also, I believe it is against the grain of CouchDB to build up more data in a reduce function than your documents contain. You’re not doing that in this case but you are following a pattern that might lead you into trouble later. It’s called reduce for a reason. 🙂

    So something like this is more the CouchDB way:

    function(doc) {
      if (doc.type == "contact") {
        emit([doc._id, 0], {
            "Name": doc.name,
            "Type": doc.type
        });
      } else if (doc.type == "phone") {
        emit([doc.contact_id, 1], {
            "Number": doc.number,
            "Type": doc.type
        });
      }
    }

    Query it for a particular contact like so:

    http://localhost:5984/testdb2/_design/testview/_view/tv1?
      startkey=[%22fc93f785e6bd8c44f14468828b00099f%22, 0]
      &endkey=[%22fc93f785e6bd8c44f14468828b00099f%22,1]
    

    Granted, you don’t get results in the same JSON structure as before but I believe this performs better within CouchDB.

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

Sidebar

Related Questions

Lets say I have two tables - child and parent with many-to-one relation. What
For simplicity lets say I have two flex mxml pages. form.mxml button.mxml If the
Let's say I have two tables: Report Comment And assuming I have a database
lets say i have two points A & B in my 3D Space now
Let's say I have two models, Classes and People. A Class might have one
Let's say I have two arrays: int ArrayA[] = {5, 17, 150, 230, 285};
Let's say I have two existing tables, dogs and cats: dog_name | owner ---------+------
Let's say we have two classes, Foo and Foo Sub, each in a different
Let's say I have two tables, news and comments. news ( id, subject, body,
Let's say you have two tables, Users and UserRoles. Here's how the two tables

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.