5
votes

Our couch DB url is http://10.1.5.88:5984/_utils/database.html?testdata/

I want to retrieve all the documents for a given key. I'm not sure whether we can search by passing the key, as a query parameter, in the above url.

Please tell me know how to display the query results via the browser.

For example: get all documents where key="countryname".

Thanks.

1
The IP in the URL you are giving is not routable, thus other people won't be able to access it.Kim Stebel
I just given my local machine ip and it won't be accessible. Please help me with your comments.user2323036
Your question is EXTREMELY unclear. Do you want to get ALL docs, by key, where the key is the countryname? Did you create a view for that? If yes, show us the code. If no, tell us if you know how view or couchdb works. We don't have a crystal ball... you know?sebster
i have not created a view for the same, however wanted to search all the documents by the given key. could you please let me know how we can do it?user2323036
Did you read the documentation? Cause what you're asking is to reproduce large parts from it.sebster

1 Answers

8
votes

You will not be able to query the database using ..?testdata&key="countryname".

To query the database to get all documents where key="countryname", you will have to first create a view in the database. See the CouchDB documentation at http://docs.couchdb.org/en/1.6.1/couchapp/views/intro.html.

I have provided an example below, if it helps:

Suppose your database contains the following documents:

{
  "_id": "708ae78bd79fa9802c827e03d3000fe3",
  "_rev": "1-f3408ffe54a9b9156dca5c9ad56ce396",
  "name": "AFGHANISTAN",
  "code": "AF"
}

{
  "_id": "708ae78bd79fa9802c827e03d3001767",
  "_rev": "1-9952b25ea39460b9923de505945efe3a",
  "name": "ALAND ISLANDS",
  "code": "AX"
}

{
  "_id": "708ae78bd79fa9802c827e03d30030a5",
  "_rev": "1-c1f3c9b2310f07f2985aa3fa363a281f",
  "name": "ALBANIA",
  "code": "AL"
}

Create a design document "_design/countries" with a view "country" in the database.

{
  "_id": "_design/countries",
  "_rev": "1-b30408713ded172df62502b45c959563",
  "views": {
    "country": {
      "map": "function(doc) {\n  emit(doc.name, null);\n}"
    }
  },
  "language": "javascript"
}

You can now query the database like this: http://127.0.0.1:5984/countries/_design/countries/_view/country?key=%22ALBANIA%22&include_docs=true

This will return you the document for Albania.

{
  "total_rows": 3,
  "offset": 2,
  "rows": [
    {
      "id": "708ae78bd79fa9802c827e03d30030a5",
      "key": "ALBANIA",
      "value": null,
      "doc": {
        "_id": "708ae78bd79fa9802c827e03d30030a5",
        "_rev": "1-c1f3c9b2310f07f2985aa3fa363a281f",
        "name": "ALBANIA",
        "code": "AL"
      }
    }
  ]
}