模拟连接在Couchbase中是如何工作的?
我有文件,一个依赖于另一个。第一:模拟连接在Couchbase中是如何工作的?,couchbase,Couchbase,我有文件,一个依赖于另一个。第一: { "doctype": "closed_auctions", "seller": { "person": "person11304" }, "buyer": { "person": "person0" }, "itemref": { "item": "item1" }, "price": 50.03, "date": "11/17/2001", "quantity": 1, "type":
{
"doctype": "closed_auctions",
"seller": {
"person": "person11304"
},
"buyer": {
"person": "person0"
},
"itemref": {
"item": "item1"
},
"price": 50.03,
"date": "11/17/2001",
"quantity": 1,
"type": "Featured",
"annotation": {
"author": {
"person": "person8597"
}
}
在这里,您可以看到doc.buyer.person依赖于其他文档,如下所示:
{
"doctype": "people",
"id": "person0",
"name": "Kasidit Treweek",
"profile": {
"income": 20186.59,
"interest": [
{
"category": "category251"
}
],
"education": "Graduate School",
"business": "No"
},
"watch": [
{
"open_auction": "open_auction8747"
}
]
}
如何从这两份文件中获取买家的姓名?我的意思是doc.buyer.person与第二个文档的id连接。它是join,从文档中不清楚 首先,让我指出您提到的文档部分的第一句话(我添加了重点): 数据之间的连接,即使正在检查的文档 包含在同一个存储桶中,不可能直接包含在 查看系统 所以,你的问题的快速答案是你有很多选择。以下是其中一些:
最后一种方法用于您在问题中链接的示例。为了提高性能,有必要根据您的使用场景调整方法。另一种解决方案是在自定义reduce函数中合并数据
// view
function (doc, meta) {
if (doc.doctype === "people") {
emit(doc.id, doc);
}
if (doc.doctype === "closed_auctions") {
emit(doc.buyer.person, doc);
}
}
// custom reduce
function (keys, values, rereduce) {
var peoples = values.filter(function (doc) {
return doc.doctype === "people";
});
for (var key in peoples) {
var people = peoples[key];
people.closed_auctions = (function (peopleId) {
return values.filter(function (doc) {
return doc.doctype === "closed_auctions" && doc.buyer.person === peopleId;
});
})(people.id);
}
return peoples;
}
然后,您可以使用“键”查询一个用户或使用“键”查询多个用户
我不知道这种方法的性能问题是什么