1

CouchDb newbie here.

I have several documents in CouchDb with the same structure:

{
    "_id": "1170140286",
    "_rev": "1-79ffad4d4cbe24effc72f9ec519373ca",
    "data": [
        {
            "photo": "link_of_photo1",
            "userid": "34623",
            "username": "guest-user1"
        },
        {
            "photo": "link_of_photo2",
            "userid": "34623",
            "username": "guest-user1"
        },
        {
            "photo": "link_of_photo3",
            "userid": "34623",
            "username": "guest-user1"
        }
    ]
}

and

{
    "_id": "43573458",
    "_rev": "1-0ca5aa68590fcb58399fe059aa8fb881",
    "data": [
        {
            "photo": "link_of_photo1",
            "userid": "6334",
            "username": "guest-user2"
        },
        {
            "photo": "link_of_photo2",
            "userid": "6334",
            "username": "guest-user2"
        },
        {
            "photo": "link_of_photo3",
            "userid": "6334",
            "username": "guest-user2"
        }
    ]
}

I don't know whether what I want to do is possible, but i am trying to create a view that will combine the data elements of these documents into one single document:

[
    {
        "photo": "link_of_photo1",
        "userid": "34623",
        "username": "guest-user1"
    },
    {
        "photo": "link_of_photo2",
        "userid": "34623",
        "username": "guest-user1"
    },
    {
        "photo": "link_of_photo3",
        "userid": "34623",
        "username": "guest-user1"
    },
    {
        "photo": "link_of_photo1",
        "userid": "6334",
        "username": "guest-user2"
    },
    {
        "photo": "link_of_photo2",
        "userid": "6334",
        "username": "guest-user2"
    },
    {
        "photo": "link_of_photo3",
        "userid": "6334",
        "username": "guest-user2"
    }
]

I am pretty sure I haven't understood the logic of couchdb correctly, so any help is highly aprpeciated.

Community
  • 1
  • 1
Krisleo
  • 21
  • 2

2 Answers2

0

What you can get is a view result with all links included. It will be an array but will look a bit different to your mocked result structure. A view logic can look like (the key of the rows is not used in the example - maybe the user name or id is an useful value to put in):

function (doc) {
  var data = doc.data
  if (!data) return

  for (var i = 0, link; link = data[i++];)
    emit(null, link)
}

For the sake of completeness should be mentioned that there is a way to "merge all docs" server-side. The view result can be manipulated by a CouchDB list before the data will finally send back to the requester. But.it.is.not.recommended! Please take that seriously and don't try that out - its a massive performance issue and was never the goal of CouchDB to provide such uses-cases.

Ingo Radatz
  • 1,225
  • 8
  • 9
-2

Please refer these links.

http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views#Linked_documents

CouchDB "Join" two documents

combine multiple documents in a couchdb view

Sample Code:

function(doc){
if(doc.items)
doc.items.forEach(function(item){
      emit(doc._id,{_id:item});
  })

}

I hope these will solve your problem.

Community
  • 1
  • 1
akhil gupta
  • 167
  • 1
  • 1
  • 11