我可以为每个数据库创建多个集合吗? [英] Can I create multiple collections per database?
问题描述
从mongo切换到pouchdb(使用Cloudant),我喜欢每个用户一个数据库的概念,但是有没有办法为每个数据库创建多个集合/表?
Switching from mongo to pouchdb (with Cloudant), i like the "one database per user" concept, but is there a way to create multiple collections/tables per database ?
示例
- Peter
- History
- Settings
- Friends
- John
- History
- Settings
- Friends
etc ...
推荐答案
Couchdb没有集合的概念。但是,将文档的类型标识符与Couchdb视图结合使用,可以达到类似的结果。
Couchdb does not have the concept of collections. However, you can achieve similar results using type identifiers on your documents in conjunction with Couchdb views.
类型标识符
将文档保存在Couchdb中时,添加一个用于指定类型的字段。例如,您将这样存储一个朋友:
When you save a document in Couchdb add a field that specifies the type. For example, you would store a friend like so:
{
_id: "XXXX",
type: "Friend",
first_name: "John",
...
}
您将这样存储历史记录:
And you would store history like this:
{
_id: "XXXX",
type: "History",
url: "http://www.google.com",
...
}
这两个文档都在同一个数据库中,如果您查询了该数据库中的所有文档,那么您将同时收到这两个文档。
Both of these documents would be in the same database, and if you queried all documents on that database then you would receive both.
视图
您可以创建按类型过滤的视图,然后直接查询这些视图。例如,创建一个视图来检索这样的朋友(在Cloudant中,您可以去添加新的设计文档,您可以直接复制并粘贴它):
You can create views that filter on type and then query those views directly. For example, create a view to retrieve friends like so (in Cloudant you can go to add new Design Document and you can copy and paste this directly):
{
"_id" : "_design/friends",
"views" : {
"all" : {
"map" : "function(doc){ if (doc.type && doc.type == 'Friend') { emit(doc._id, doc._rev)}}"
}
}
}
让我们扩展地图功能:
function(doc) {
if (doc.type && doc.type == "Friend") {
emit(doc._id, doc._rev);
}
}
基本上,此地图功能是说仅将文档关联到该视图具有== Friend类型。现在,我们可以查询此视图,并且只会返回朋友:
Essentially this map function is saying to only associate documents to this view that have type == "Friend". Now, we can query this view and only friends will be returned:
http://SERVER/DATABASE/_design/friends/_view/all
其中朋友
=名称设计文档和 all
=视图名称。将 SERVER
替换为服务器,将 DATABASE
替换为数据库名称。
Where friends
= name of the design document and all
= name of the view. Replace SERVER
with your server and DATABASE
with your database name.
您可以在此处找到有关视图的更多信息:
You can find more information about views here:
https:/ /wiki.apache.org/couchdb/Introduction_to_CouchDB_views
这篇关于我可以为每个数据库创建多个集合吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!