couchdb icon indicating copy to clipboard operation
couchdb copied to clipboard

Key value doesn't work as json, only as string

Open Pragrammist opened this issue 11 months ago • 4 comments

Description

When you pass "key" query to GET /{db}/_design/{ddoc}/_view/{view} for example /your_db/_design/your_doc/_view/some_view?key="some_exist_key" works fine, it return rows if it match. But if you use json as key: /your_db/_design/your_doc/_view/some_view?key={"json_example": "some_exist_key"} for example will not return any row even if {"json_example": "some_exist_key"} as key exist.

Steps to Reproduce

I use it

http://admin:admin@localhost:5984/account_documents/_design/unique_data_design_doc/_view/account_id_rev_by_hash_password_login?key={"login": "login_2","password_hash": "XKJyibT/PjDRPy2X/DP86v12ZmzKdHuVudId5E0+4ts"}

with view

function (doc) {
  emit({login:doc.login, password_hash:doc.password_hash}, [doc._id, doc._rev]);
}

If you want reproduce bag, just add document and then view with json as key and then try retrieve by then key row, you got empty row array

Expected Behaviour

Possibility to retrieve document by json not only by string, so key like

{"login": "login_2","password_hash": "XKJyibT/PjDRPy2X/DP86v12ZmzKdHuVudId5E0+4ts"} 

will return not empty array rows if key is exists

Your Environment

I think it's not matter TIP: # ( Include as many relevant details about your environment as possible. ) TIP: # ( You can paste the output of curl http://YOUR-COUCHDB:5984/ here. )

  • CouchDB version used: 3.4.2
  • Browser name and version: edge, postman
  • Operating system and version: windows 10

Additional Context

Pragrammist avatar Jan 12 '25 11:01 Pragrammist

~This is not functionality that CouchDB supports. What made you think this would work?~ I misread the question, apologies.

janl avatar Jan 14 '25 15:01 janl

Well, startkey and endkey support json as value. And, important, if you read couchdb doc you see key is json, not a string.

https://docs.couchdb.org/en/stable/api/ddoc/views.html

And if it's not a json, why there's it's described as json, not a string. I spent several hours because of that Did you read couchdb doc?!

Pragrammist avatar Jan 15 '25 03:01 Pragrammist

hm, odd. I definitely expect exact lookup to work for a key that is a json object. It worked for me locally and is definitely intentionally supported. The collation specification even details the ordering that we apply for json objects (https://docs.couchdb.org/en/stable/ddocs/views/collation.html#collation-specification).

Now, note that you would have to match the key, you couldn't find a row with key of {"foo":"bar", "bar":"baz"} with just ?key={"foo":"bar"}.

rnewson avatar Jan 15 '25 17:01 rnewson