JVK
JVK

Reputation: 3912

Couchdb views generation

I am learning couchdb, so I have a very basic questions. Suppose I have a couch database called email where documents look like:

{
  "_id": "fe7759fdf36t",
  "status": "sent",
  "sent_at": 1357151443,
}

{
  "_id": "ewrwe4a38d2353",
  "status": "failed",
  "sent_at": 2347151421,
}

{
  "_id": "f4559fjffd2353",
  "status": "sent",
  "sent_at": 11715154,
}

I want to create two views

1) Retrieve all documents where status = sent
2) Retrieve all documents where status = failed and sent_at > 11715157
3) Retrieve all documents sorted by sent_at descending/ascending and status = sent

For (1), I wrote,

{
    "_id": "_design/sentdoc",
    "_rev": "2-dd88de7196c06eed41d2d83a958f0388",
    "views": {
        "query": {
            "map": "function(doc) { if(doc.status == 'sent') {emit(doc_id, doc);} }"
        }
    }
}

I guess I am doing something wrong there, so it is not working. Can anyone suggest how to write all these three views?

Upvotes: 0

Views: 131

Answers (1)

tobinharris
tobinharris

Reputation: 2559

Create a view like this

function(doc){
  emit([doc.status, doc.sent_at], doc); 
}

Query #1

Try these params in your curl statement (or Ruby library or whatever)

startkey = ["sent"]
endkey = ["sent",{}]

Query #2

startkey = ["failed", 11715157]
endkey = ["failed", {}]

Query #3

startkey = ["sent"]
endkey = ["sent", {}]
descending = true

I've not tested this, but should point you in the right direction.

Upvotes: 1

Related Questions