How can I identify all possible CouchDB database keys?

I am creating one application where for each product I have one database and I will create another document based on the date. The keys in the documents may vary and depend on the user that he provides. Suppose the user will give the same tracking key with a changed value over time. In the end, I need to know all the possible keys before creating automatic views on them.

Example: If I had a database, say, a test. It contains, say, two documents,

  1. {
  "_id": "1",
  "_rev": "1-"
  "type": "Note",
  "content": "Hello World!"
 }

 2. {
  "_id": "2",
  "_rev": "1-"
  "type": "Note",
  "content": "Beyond Hello World!",
  "extra": "Boom"
 }

Then I want to list all the keys in this database. So the answer should be _id, _rev, type, content and extra.

These keys are dynamic and user dependent. Therefore, I could not assume that I knew them beforehand.

+4
source share
1 answer

I have never used stackoverflow before, I saw your question trying to solve this problem myself, so I signed up. I think this solves your problem:

create a view in which "views" includes the following:

{"keys": {"map": "function (doc) {for (var thing in doc) {emit (thing, 1);}}", "reduce": "function (key, values) {return amount ( values);} "}}

then the request in this view with group = true, for example:

http: // localhost: 5984 / mydb / _design / myview / _view / keys? group = true

You should return a list of all the keys in your database and calculate how often this happens.

Does it help?

+8
source

All Articles