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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:03:39+00:00 2026-05-16T03:03:39+00:00

I am starting to study map-reduce databases. How can one implement a reference in

  • 0

I am starting to study map-reduce databases. How can one implement a reference in a map-reduce database, such as CouchDB or MongoDB? For example, suppose that I have drivers and cars, and I want to mark that some driver drives a car. In SQL it’s something like:

SELECT person_id, car_id FROM driver, car WHERE driver.car = car.car_id

(That is, if my memory serves right – I haven’t programmed in SQL for a while.)

In languages that has references it is ever simpler: An instance of Person can point to instances of Car.

What is the map-reduce equivalent to this sort of relations?

  • 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-16T03:03:39+00:00Added an answer on May 16, 2026 at 3:03 am

    In CouchDB you would write a map/reduce that outputs ALL of the cars and drivers with complex keys, and then use key ranges to pick both. For example, let’s assume your documents look like these two…

    {
      "_id": "...",
      "_rev": "...",
      "docType": "driver"
    }
    
    {
      "_id": "...",
      "_rev": "...",
      "docType": "car",
      "driver": "driver's _id"
    }
    

    You could use duck typing instead of specifying the docType, but I like this method better.

    Your map function:

    function(doc)
    {
      if(doc.docType == "driver")
        emit([doc.id, 0], doc);
      elseif(doc.docType == "car")
        emit([doc.driver, 1], doc];
    }
    

    Our complex key is an array, with the first item always being the driver’s _id. The second item in the array prevents key collision, and allows us to reference the car or driver directly (more on this later).

    We can now use the key range query parameters to grab both of the docs.

    ?startkey=["driver _id"]&endkey=["driver _id", {}]
    

    This basically says “give me any array with the driver _id as the first item, and anything in the second. This works because objects – the second item in the endkey‘s array – is sorted as the highest. See http://wiki.apache.org/couchdb/View_collation?redirect=ViewCollation#Collation_Specification for more information about how items get sorted/weighed in keys.

    This also scales quite nicely, because we can add more information into our map function without having to change our query in the client. Let’s say we add a sponsor docType: we just add another elseif for the docType field and then emit([doc.driver, 2], doc);. Now we can pull all three documents in one request with the same key range query from above.

    Of course, you can also specify individual documents instead of pulling all of them. ?key=["driver's _id", 1] would pull just the car for the specified driver.

    Cheers.

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

Sidebar

Related Questions

I am starting now to study Android. I came from iOS development. How can
i am starting to study FLEX. i have only flex sdk. So can i
Just starting to study the Azure framework. Just say you create a row in
Starting to learn Canvas and have two classes so far (main one to call
Starting with a list of objects containing two parameters notional and currency, how can
I'm starting study opengl, and im tring to make a 3d chess like, but
Starting iOS development, I wish to implement handwriting recognition in my app. I did
I am starting study java2me. But I have some source study about it like
I am starting to study some web technologies to integrate content, markup, layout, styling
I'm starting to study EF4 as a possible replacementt for NH in my new

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.