我正在执行一个操作,它可以工作,但我想知道是否有更好或更有效的方式来做我想做的事情。
我的数据库中有一个对象,如下所示:
{
"id": "testId",
"name": "testName",
"products": [
{
"name": "product1"
"info": "sampleInfo",
"templateIds": [
"asdf-1",
"asdf-2"
]
},
{
"name": "product2"
"info": "sampleInfo",
"templateIds": [
"asdf-1",
"asdf-2"
]
}
]
}如您所见,"products“数组中的每个”products“都有一个templateIds的子数组。这些匹配模板存储在另一个表中。我想要做的是创建一个查询,将这些模板合并到每个产品对象上,然后再将其全部发回。
目前,我正在用潜水艇做这件事:
r.table('suites').get('testId').merge(function(suite){
return {
products: suite('products').merge(function(product){
return {
templates: r.expr(product('templateIds')).map(function(id) {
return r.table('templates').get(id)
})
}
})
}
})我的问题是:有没有更有效的方法来做到这一点?还是我应该用一种完全不同的方式来做这件事呢?
谢谢你们!
发布于 2014-10-24 17:37:03
在我看来是对的。我唯一能想到的是,r.table('templates').get_all(r.args(product('templateIds')))比product('templateIds').map(function(id){ return t.table('templates').get(id);})短,而且很可能更快。
编辑:如果你有少量的模板,另一件可以让它运行更快的事情是在客户端执行替换,然后用ID缓存检索到的模板。RethinkDB必须对每个模板ID进行单独的读取,即使它一遍又一遍地看到相同的模板ID,因为它不知道缓存这些值是否安全。
https://stackoverflow.com/questions/26553206
复制相似问题