如何确定CouchDB数据库的所有可能密钥? [英] How could I determine all possible keys of a CouchDB database?

查看:84
本文介绍了如何确定CouchDB数据库的所有可能密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,其中每个产品都有一个数据库,并且我将根据日期创建不同的文档.文档中的密钥可能会有所不同,并且取决于用户及其提供的内容.假设用户将不断提供相同的密钥以进行随时间变化的值的跟踪.最后,我需要先了解所有可能的键,然后才能在它们上创建自动视图.

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"
}

然后,我要列出此数据库中的所有键.因此,答案应为_id,_rev,类型,内容等.

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.

推荐答案

我以前从未使用过stackoverflow,在尝试自己解决此问题时看到了您的问题,因此我已经注册.我认为这可以解决您的问题:

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:

{ 键":{ "map":"function(doc){表示(doc中的var something){发射(thing,1);}}", "reduce":函数(键,值){返回和(值);}" } }

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

然后使用group = true对该视图进行查询,例如:

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.

这有帮助吗?

这篇关于如何确定CouchDB数据库的所有可能密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆