您似乎无法在CouchDB中嵌套数据库.人们如何解决这个限制?例如,假设我想创建一个博客引擎,其中每个域都有一个单独的数据库.在每个数据库中,我可能希望用户数据库,订单数据库等包含各种用户文档,订单文档等.
显而易见的方式似乎是一个扁平结构,其中数据库名称用连字符划分数据库嵌套级别之间的人为边界:
myblog.com-users
myblog.com-posts
myblog.com-comments
anotherblog.com-users
anotherblog.com-posts
anotherblog.com-comments
...hundreds more...
Run Code Online (Sandbox Code Playgroud)
另一种解决方案是保留较低级别的数据库并使用顶级值标记每个文档:
包含文档User1的用户数据库,字段instance ="Test"或字段domain ="myblog.com"
我认为你在这里滥用了数据库一词.没有理由不能将用户,帖子和评论数据存储在单个couchdb数据库中.您的couchdb视图可以从评论文档中分离出帖子文档中的用户文档.
couchdb数据库中用户文档的示例映射函数:
function(doc) {
if (doc.type = 'user') { // only return user documents
emit([doc.domain, doc.id], doc); // the returned docs will be sorted by domain
}
}
Run Code Online (Sandbox Code Playgroud)
有关使用startkey和endkey以及视图排序规则限制视图结果的方法,请参阅查看Api.
| 归档时间: |
|
| 查看次数: |
1966 次 |
| 最近记录: |