javascript - CouchDB view composing JSON objects with embedded arrays from two separated documents -


Say I have two types of documents that are stored in my couchdib database. First, contact Is the property property set on and second to phone . The contact type document has another asset as the name. The phone type has the number of properties and contact_id, so that it is in the context of the contact person. This is one of the many scenarios, where one contact can have an N phone number (I know that they can be embedded in a single contact document, but I want to display multiple relationships with different documents Will happen).

Scott 2 phone provide data with the number and Matt 1 Number:

  {_ id: "fc93f785e6bd8c44f14468828b001109", _rev: "1-fdc8d121351b0f5c6d7e288399c7a5b6", type: "call ", Number:" 465789 ", contact_id:" fc93f785e6bd8c44f14468828b00099f "} {1} _ id: "fc93f785e6bd8c44f14468828b00099f", _rev: "1-BD 643 A 6 B 0 A 090 9 7 A 42 D 8 C 0 B 0 A 0 C 0 9 7 A 42 D 8 C 04 C 5 C 0 9 6 9" type: "Contact", the name "Scott"} {_id: "16309fcd03475b9a2924c61d690018e3", _rev: "1-723b7c999111b116c353a4fdab11ddc0", as follows: "Contact", not M: "math"} {_id: "16309fcd03475b9a2924c61d69000aef", _rev: "3-67193f1bfa8ed21c68e3d35847e9060a", type: "phone", number: "789,456", contact_id: "16309fcd03475b9a2924c61d690018e3"}  

Map function:

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

Reduce function:

  function (keys, values) {var output = {}; (Value [ELEM]. (Value [ELEM] .type == "contact") {product = {"id": value [ELEM] ._ id, "name": value [ELEM] .name, "Type": Value [AMM]. Type, "phone": []}; } And if (value [AMM]. Type == "phone") {output. Phone pash ({"number": value [AMM] .umber, "type": value [AMM] type)); }} Return output; }  

Due to keys in the MAP function, group_level is set to 1.

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

or startkey and search for some contacts with endkey like this:

  http: // localhost: 5984 / testdb2 / _design / testview / _view / TV1 group_level = 1 & amp; Startkey = [% 22fc93f785e6bd8c44f14468828b00099f% 22] & amp; endkey = [% 22fc93f785e6bd8c44f14468828b00099f% 22, {}}  

The result of course I want to - will be embedded phones as many relationships with contacts. And here's the question: Is this the correct way to use the Maps radius function in COEDBB? Are there any significant performance problems while using this approach?

Usually you do not speak using less disk space. emit (.. ., Doc) .

All of you may want to reconsider to reduce the function. It is not really necessary to get the data you need, for example, with the following lines, you can use some less disk space and perform better if you have too many records.

In addition to this, I believe that it is against CouchDaddy's grain to make more data, reduce the function compared to your documents. You are not doing this in this case, Patterns are following which you may have trouble later on. This is called less for a reason: -)

Then there is more CouchDB method like this:

 function (doc) {if (doc.type == "Contact") {Throwing ([doc._id, 0], {"name": doctor name, "type": doc.type}); } Else if (doc.type == "phone") {emit ([doc.contact_id, 1], {"number": doc.number, "type": doc.type}); }}   

Ask it as a special contact:

 http: // localhost: 5984 / testdb2 / _design / testview / _view / tv1? Initial = [% 22fc93f785e6bd8c44f14468828b00099f% 22, 0] and Andki = [% 22fc93f785e6bd8c44f14468828b00099f% 22,1] 

Granted, you do not get results in the same JSON structure before, but I believe It performs better within CouchDB.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -