PRASANTHMV
PRASANTHMV

Reputation: 299

How To Get Parent Details In Couch Db?

Please Help Me

I have Created A databse named Product

And I have created 2 product documents with id 'P101','P102'

{ "_id": "P101", "_rev": "5-8d1c4b5b7f6e65a5997d4ec5b4346e5f", "type": "product", "name": "Shirt", "hersteller": "oska", "price": 11.15 }

{ "_id": "P102", "_rev": "3-8ad956f0c524b1a2396ebdda1ed0afbe", "type": "product", "name": "Pullover", "hersteller": "acme", "price": 7.58 }

Now added one Order document with 'P101' as its parent

{ "_id": "ORD-101", "_rev": "7-dbd8cca3b6dc9f4cb3b8bb7e4aec6630", "type": "order", "orderDate": "01.01.2012", "parentProdct": "P101" }

Now I have added the Report document with 'ORD-101' as its parent

{ "_id": "REPORT-101", "_rev": "10-c57e3c039a44d95d6bd40d8a83a951da", "type": "REPORT", "parentOrder": "ORD-102" }

After this all I have added the design document with corresponding view functions

{ "_id": "_design/proddesign", "_rev": "87-d1b0a8c8cdfe569bbf9805df4918af4f", "rev": "3-productrev", "views": {
"fifth": { "map": "function(doc){if(doc.type=='REPORT'){emit(doc._id,{_id:doc.reportDet});}}" } } }

.. This give me only the Parent off Report document.Actually I want to print the details of corresponding parent of order document also.

ie details 'P101' also..

How Can I achive that ..Pls Help

Upvotes: 0

Views: 320

Answers (1)

Eli Stevens
Eli Stevens

Reputation: 1447

Assuming the following data (note: _rev and unused docs removed from your example):

{ 
  "_id": "P101", 
  "type": "product", 
  "name": "Shirt", 
  "hersteller": "oska", 
  "price": 11.15 
}

{ 
  "_id": "ORD-101", 
  "type": "order", 
  "orderDate": "01.01.2012", 
  "product": "P101" 
}

{ 
  "_id": "REPORT-101", 
  "type": "report", 
  "order": "ORD-101",
  "product": "P101" 
}

You'll want to do something along the lines of:

if (doc.type == "product")
    emit([doc._id], doc.type)
if (doc.type == "order")
    emit([doc.product, doc._id], doc.type)
if (doc.type == "report")
    emit([doc.product, doc.order, doc._id], doc.type)

Which will result in view output like:

[
    [["P101"], "product"], 
    [["P101", "ORD-101"], "order"], 
    [["P101", "ORD-101", "REPORT-101"], "report"]
]

Which if you query with

?startkey=["P101"]&endkey=["P101",{}]

Will give you the product P101 as well as all of the orders and reports for that product.

Upvotes: 0

Related Questions