How could I determine all possible keys of a CouchDB database?

拈花ヽ惹草 提交于 2019-12-07 08:08:07

问题


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.


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!