简体   繁体   中英

Join two different documents in couchdb using futon map program?

I have two documents like below :

{
   "_id": "00315fd5428d43a19ce86ae7a273085f",
   "_rev": "3-031e613837bf7e9c75f8bc9f5230d921",
   "device_sno": "WEC343",
   "moving_status": true,
   "overall_status": true,
   "temperature": "-1° C",
   "trip_id":3
}

{
  "_id": "1a",
  "_rev": "4-e876b14dcb4cfd18aba8eb36b2be33bd",
  "customer_name": "praneeth",
  "customer_id": 2,
  "tripp_id": 3
}

Now based on trip id in first doc, I want to get the customer name from doc2. can any one help me to fix this, it would great help to me..........

You can do this using couchdb linked documents .

If you emit an object value which has {'_id': XXX} then include_docs=true will fetch the document with id XXX rather than the document which was processed to emit the key/value pair.

This means that if one document contains the ids of other documents, it can cause those documents to be fetched in the view too, adjacent to the same key if required.

If you change the schema of your documents a bit and include the _id field of the second document in the first document like so

{....,{"trip_id":"id of the document to fetch"}}

Then you can define a view like

function(doc){
 emit(doc.key,{"_id":doc.trip_id});//doc.key is the key on which you want your view to be sorted.
}

now you can query like so

http://localhost:5984/db_name/_design/ddoc_name/_view/view_name?key="some-value"&include_docs=true

this will fetch the linked document. The important point to note here is the second parameter in the emit must point to the document that you want to fetch. In your case trip_id must point to the _id of the second document.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM