在 RethinkDB 中查询复合多索引
Querying a compound multi-index in RethinkDB
我正在尝试有效地检索以下格式的数据:
{
"datetime": "1453845345493",
"someIds": ["id2000-4", "id1000-34", "id2000-43", "id250-34"]
}
具体来说,我想做的是"find all records that have happened since a given time, and of those, return any that have one or more of a list of Ids."
到目前为止,我按照 shown here 方法使用以下方法创建复合多索引:
r.db("dbName").table("tableName")
.indexCreate(
"idDatetime",
function(each) {
return each("someIds").map(function(id){
return [each("datetime"), id]
})
}
,{multi: true})
这成功地根据类似于 ["1453845345493", "id2000-4"]
的值构建了索引
但现在感觉有点太深了,实际上不知道如何使用该索引进行查询来完成上面的objective。您如何设计该查询?
如果你还没有,你绝对应该 check out this page on multi-indexes 在我们的文档中。
使用您显示的数据的示例如下:
r.table("tableName").getAll("id2000-4", {index: "idDatetime"}).run(conn, callback)
这应该会为您提供 table 中包含 id2000-4
的所有文档。
让我知道这是否适合您!
我想(我可能错了),基本上,我们有两种类型的索引查找:
- 完全匹配:
get
、getAll
- 范围匹配:
between
所以在你的情况下,很明显我们不能使用 getAll
因为你想 find all records that have happened since a given time, and of those, return any that have one or more of a list of Id
.
那只剩下我们 between
。因此,让我们找到一种对其进行建模的方法。
我建议将 datetime
字段更改为数字而不是字符串。我猜你正在存储纪元。
我们将像您一样创建索引:
r.table('t11').indexCreate('idDatetime', function(doc) {
return doc('someIds').map(function(id){
return [doc("datetime"), id]
})
}, {multi: true})
然后我们查询类似这样:
r.table('t11')
.between([1453845345493, "id1000-34"], [r.maxval, "id1000-34"], {index: 'idDatetime'})
查找自该纪元以来包含 id1000-34
的所有文档。您可以使用 JavaScript 或 RethinkDB 日期时间函数找到昨天的纪元。
更新:
虽然它并不完美,但我们可以像这样模拟任何一个 id:
r.expr(["id1000-34", "id1000-4"])
.concatMap(function(needle) {
return r.table('t11')
.between([1453845345499, needle], [r.maxval, needle], {index: 'idDatetime'})
})
.distinct()
我正在尝试有效地检索以下格式的数据:
{
"datetime": "1453845345493",
"someIds": ["id2000-4", "id1000-34", "id2000-43", "id250-34"]
}
具体来说,我想做的是"find all records that have happened since a given time, and of those, return any that have one or more of a list of Ids."
到目前为止,我按照 shown here 方法使用以下方法创建复合多索引:
r.db("dbName").table("tableName")
.indexCreate(
"idDatetime",
function(each) {
return each("someIds").map(function(id){
return [each("datetime"), id]
})
}
,{multi: true})
这成功地根据类似于 ["1453845345493", "id2000-4"]
但现在感觉有点太深了,实际上不知道如何使用该索引进行查询来完成上面的objective。您如何设计该查询?
如果你还没有,你绝对应该 check out this page on multi-indexes 在我们的文档中。
使用您显示的数据的示例如下:
r.table("tableName").getAll("id2000-4", {index: "idDatetime"}).run(conn, callback)
这应该会为您提供 table 中包含 id2000-4
的所有文档。
让我知道这是否适合您!
我想(我可能错了),基本上,我们有两种类型的索引查找:
- 完全匹配:
get
、getAll
- 范围匹配:
between
所以在你的情况下,很明显我们不能使用 getAll
因为你想 find all records that have happened since a given time, and of those, return any that have one or more of a list of Id
.
那只剩下我们 between
。因此,让我们找到一种对其进行建模的方法。
我建议将 datetime
字段更改为数字而不是字符串。我猜你正在存储纪元。
我们将像您一样创建索引:
r.table('t11').indexCreate('idDatetime', function(doc) {
return doc('someIds').map(function(id){
return [doc("datetime"), id]
})
}, {multi: true})
然后我们查询类似这样:
r.table('t11')
.between([1453845345493, "id1000-34"], [r.maxval, "id1000-34"], {index: 'idDatetime'})
查找自该纪元以来包含 id1000-34
的所有文档。您可以使用 JavaScript 或 RethinkDB 日期时间函数找到昨天的纪元。
更新:
虽然它并不完美,但我们可以像这样模拟任何一个 id:
r.expr(["id1000-34", "id1000-4"])
.concatMap(function(needle) {
return r.table('t11')
.between([1453845345499, needle], [r.maxval, needle], {index: 'idDatetime'})
})
.distinct()