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 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

Ask A Question

Stats

  • Questions 532k
  • Answers 532k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The reason why is that while both 0 and null… May 17, 2026 at 12:14 am
  • Editorial Team
    Editorial Team added an answer Assuming you're using .NET 3.5 or higher, you need to… May 17, 2026 at 12:14 am
  • Editorial Team
    Editorial Team added an answer Session variables are always kept and protected at the server.… May 17, 2026 at 12:14 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

i am starting to study FLEX. i have only flex sdk. So can i
I am starting study java2me. But I have some source study about it like
I'm starting study opengl, and im tring to make a 3d chess like, but
Just starting to learn scala.. I can't seem to figure out how to compile
I usually write web apps in PHP, Ruby or Perl. I am starting the
If there is browse_by_exam or my_study_centre in the 1st position starting from project_name as
I like to study languages outside my comfort zone, but I've had a hard
As a starting programmer with little industry-experience, I find it frustrating to learn a
I'm doing a study on large Java projects and would like to view the
I begin to study qt (I have qt creator, last version), so I read

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.