我有一个noSql (云)数据库
在数据库中,我们有文档,其中一个文档字段表示“-Within”(文档类型)
-Within文档,我们有表示数据库中其他文档的链接的字段
例如:
{_id: 111, table:main, user_id:222, field1:value1, other1_id: 333}
{_id: 222, table:user, first:john, other2_id: 444}
{_id: 333, table:other1, field2:value2}
{_id: 444, table:other2, field3:value3}我们需要一种搜索_id:111的方法
结果是一个包含来自链接表的数据的文档:
{_id:111, user_id:222, field1:value1, other1_id: 333, first:john, other2_id: 444, field2:value2, field3:value3}有没有办法做到这一点?
在我们如何存储或获取数据的结构上有灵活性-有什么建议如何更好地组织数据以使其成为可能?
发布于 2017-05-07 14:26:57
首先要说的是,Cloudant中没有连接。如果您的模式依赖于大量的连接,那么您的工作就与Cloudant的粒度相反,这可能会给您带来额外的复杂性或性能影响。
有一种方法可以在MapReduce视图中取消引用其他文档的in。下面是它的工作原理:
中拉回文档和取消引用的in
在您的示例中,map函数如下所示:
function(doc) {
if (doc.table === 'main') {
emit(doc._id, doc);
if (doc.user_id) {
emit(doc._id + ':user', { _id: doc.user_id });
}
}
}将允许您通过点击GET /mydatabase/_design/mydesigndoc/_view/myview?startkey="111"&endkey="111z"&include_docs=true端点在一个API中拉回主文档及其链接的用户文档:
{
"total_rows": 2,
"offset": 0,
"rows": [
{
"id": "111",
"key": "111",
"value": {
"_id": "111",
"_rev": "1-5791203eaa68b4bd1ce930565c7b008e",
"table": "main",
"user_id": "222",
"field1": "value1",
"other1_id": "333"
},
"doc": {
"_id": "111",
"_rev": "1-5791203eaa68b4bd1ce930565c7b008e",
"table": "main",
"user_id": "222",
"field1": "value1",
"other1_id": "333"
}
},
{
"id": "111",
"key": "111:user",
"value": {
"_id": "222"
},
"doc": {
"_id": "222",
"_rev": "1-6a277581235ca01b11dfc0367e1fc8ca",
"table": "user",
"first": "john",
"other2_id": "444"
}
}
]
}注意我们是如何返回两行的,第一行是主文档体,第二行是链接用户。
https://stackoverflow.com/questions/43825864
复制相似问题