4

I am creating one application where for every product I have one database and I will create different document based on date. The keys in documents could be different and depend upon user, what he provides. Assumption is user will keep giving same key for tracking with changed value over time. In the end, I need to know all possible keys before creating automatic views on them.

Example: If I had DB, say, 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 keys in this DB. So, answer should be _id,_rev,type,content and extra.

These keys are dynamic and depend upon users. So, I couldn't assume that I knew them in advance.

Gagandeep Singh
  • 5,755
  • 4
  • 41
  • 60
  • Please could you include some more information like example documents. It's hard to understand the question I'm afraid. – James C May 30 '11 at 14:36
  • @james-c I added example as you requested. Hopefully it will help in understanding the problem. Please let me know if you need any extra info. – Gagandeep Singh May 31 '11 at 08:44
  • I do not understand what you are trying to do, however I think the predefined view `_all_docs` should give you all the info you want. Query it with http://localhost:5984/DBNAME/_all_docs. All usual view query parameters do apply. – Marcello Nuccio May 31 '11 at 14:19
  • @Marcello I need to create separate views on each of these distinct keys. I can't tell what I need this for. The [localhost:5984/DBNAME/_all_docs](localhost:5984/DBNAME/_all_docs) only give all the data of DBNAME. I can compute distinctive keys from this but once DB become large enough then doing this operation wouldn't make sense. That's why I need map/reduce function that can do this magic for me. And, since couchdb has incremental map/reduce already in place. It would be smaller operation in latter stages also. – Gagandeep Singh May 31 '11 at 16:39

1 Answers1

8

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

create a view where "views" includes this:

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

then query on that view with group=true e.g.:

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

you should get back a list of all the keys in your database and a count of how often the occur.

does this help?

mark
  • 96
  • 1
  • by the way, I only just came up with this as I did not find a solution - I have not checked to consider the performance of this. – mark Jun 24 '11 at 17:55