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.
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?
来源:https://stackoverflow.com/questions/6174984/how-could-i-determine-all-possible-keys-of-a-couchdb-database