How to implement links in databases with map reduction?

I am starting to explore databases that simplify the mapping. How can I implement a link in a map-reducing database, such as CouchDB or MongoDB? For example, suppose I have drivers and cars, and I want to note that some driver drives a car. In SQL, it is something like:

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

(That is, if my memory works correctly - I have not programmed in SQL for a while.)

In languages ​​where there are links, everything is simpler: an instance of Person can point to instances of Car.

What is a display reduction equivalent to this relationship?

+3
source share
2 answers

CouchDB /, , . , ...

{
  "_id": "...",
  "_rev": "...",
  "docType": "driver"
}

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

duck typing docType, .

:

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

- , _id. ( ).

, .

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

: " _id - . , - endkey - . . http://wiki.apache.org/couchdb/View_collation?redirect=ViewCollation#Collation_Specification , / .

, , . , docType: elseif docType, emit([doc.driver, 2], doc);. .

, , . ?key=["driver _id", 1] .

.

+2

, , . , . ; .

{
  "_id": "joe_the_driver",
  "name": "Joe",
  "cars": [
    { "_id": "123-AB", /* car properties */ },
    { "_id": "456-YZ", /* car properties */ }
  ]
}

" ". --, :

{
  "_id": "joe_the_driver",
  "car_ids": [ /* ID that refer to car documents */ ]
}

{
  "_id": "123-AB",
  "driver_ids": [ /* ID that refer to driver documents */ ]
}

, , SQL. .

+1

Source: https://habr.com/ru/post/1758175/


All Articles