mongodb - How to implement references in map-reduce databases? -
I am starting to study the database to reduce how I can apply a context in a map - the database Can reduce, such as couchdiby or mangodibi? For example, suppose I have a driver and a car, and I have to remember that some drivers drive. There is something like this in SQL:
select viewer_id, driver by car, car where driver .car = car.car_id
(That is, if my memory Works right - I have not programmed in SQL for some time.)
There are references in languages, it is sometimes easier: an example of a person can point to examples of the car.
What is the map of this kind of relationship?
You will write a map in CouchDbie / reduce the output of all cars and drivers with a complex key , And then use the major categories to choose both. For example, suppose that your documents look like these two ...
<">": "...", "_rev": "...", " "Driver" _id "" driver "_id" "driver" _id ":" driver "_id": "...", "_rev": "...", "doctor type": "car", "driver":Your map function:
function (doctor) {pre}
Emitted ([doc.docType = "" driver ") ([doc.id, 0], doctor); fidf (doc.docType ==" car ") emit ([doc.driver, 1], doc];}
Our complex key is an array, the first item always prevents the second item key collision in the _id array of the driver, and allows us to refer directly to the car or driver (and later on more).
Now we can use the key range query parameter to capture both of the docs.
? Startkey = ["driver_id"] and endki = ["driver_id", {}]
This is basically "the driver of _id as the first item With no array, and anything else in the other. This works because the second item in the array of objects - endkey
- is sorted as the highest. See more about how things are sorted / keys are weighed in. Information about this.
This is also quite well scales, because we can add more information to the client without changing our query. Doctor type: We just add another elseif
for a docType field and then
Of course, you can specify individual documents instead of dragging them all. ? Key = ["_id of the driver", 1]
will just pull the car for the specified driver.
Cheers.
Comments
Post a Comment