pe8ter
pe8ter

Reputation: 1263

Cannot get `key` query string parameter to select a specific row

Been banging my head against a wall here. I can't get the key query string parameter to select a specific row when querying for documents. Here's a script to run against a fresh database that shows I can query all the documents, but cannot select just one by its key:

#!/usr/bin/env bash -ex

# Create a database.
curl -X PUT http://localhost:5984/names

# Add a few documents.
curl -X POST -H 'Content-Type: application/json' -d '{"name":"joe"}' http://localhost:5984/names
curl -X POST -H "Content-Type: application/json" -d '{"name":"frank"}' http://localhost:5984/names

# Add a view function.
curl \
    -X PUT \
    -H 'Content-Type: application/json' \
    -d '{ "views": { "by_name": { "map": "function (doc) { if (doc.name) { emit(doc.name, doc.name.toUpperCase()) } }" } } }' \
    http://localhost:5984/names/_design/docs

# Querying all the documents works.
curl -X GET http://localhost:5984/names/_design/docs/_view/by_name

# Selecting one of those documents by key does not work.
curl -X GET http://localhost:5984/names/_design/docs/_view/by_name?key=frank

As far as I can tell I'm following the example in the first paragraph of the "Find One" section of the CouchDB docs.

Upvotes: 0

Views: 273

Answers (1)

Dominic Barnes
Dominic Barnes

Reputation: 28439

Your key should be JSON-encoded, so use key="frank" instead. (just like in the example)

Upvotes: 1

Related Questions