我开始使用RethinkDB,我在理解正在运行的一些查询时遇到了什么问题.
问题是使用暴露JSON API 的NodeJS应用程序(使用rethinkdbdash驱动程序)查询具有日期时间类型的对象 .如果我单独查询我的对象,如:
db.table('apples').max('timestamp');
Run Code Online (Sandbox Code Playgroud)
我获取时间戳字段的JavaScript日期,而如果我在查询中运行group by运算符,如:
db.table('apples').group('type').max('timestamp');
Run Code Online (Sandbox Code Playgroud)
我得到相同时间戳字段的日期时间假型(我认为这是正确的名称).就像是:
{
"$reql_type$": "TIME",
"epoch_time": 1423077646.772,
"timezone": "-07:00"
}
Run Code Online (Sandbox Code Playgroud)
这是预期的行为吗?它背后的逻辑是什么?
我想在我的JSON中返回序列化的iso8601日期,而不是这种数据类型,以使解析对客户端透明.
谢谢,
更新 这是2.2.7之前版本的已知问题.固定在这里.
链式有什么区别:
r.db('catbox').table("bw_mobile").filter(
r.row("value")("appVersion")("major").le(2)
).filter(
r.row("value")("appVersion")("minor").le(2)
).filter(
r.row("value")("appVersion")("patch").le(10)
)
Run Code Online (Sandbox Code Playgroud)
嵌套:
r.db('catbox').table("bw_mobile").filter(
r.row("value")("appVersion")("major").le(2).and(
r.row("value")("appVersion")("minor").le(2).and(
r.row("value")("appVersion")("patch").le(10)
)
)
)
Run Code Online (Sandbox Code Playgroud)
或 lambda 函数
r.db('catbox').table("bw_mobile").filter(
r.js("(function (session) {
return session.value.appVersion.major < 0
|| ( session.value.appVersion.major == 0 && session.value.appVersion.minor < 0 )
|| ( session.value.appVersion.major == 0 && session.value.appVersion.minor == 0 && session.value.appVersion.patch < 71 )
;
})")
)
Run Code Online (Sandbox Code Playgroud)
泰!
是否可以执行大量upsert,其行为使得特定字段(例如createdDT:)仅在第一次upsert时设置,而不是在同一记录的后续upsert期间设置?
编辑
有一个原始版本工作,它执行服务器端执行循环,其中每个元素的查找后跟一个条件插入.不是最优雅,也可能是相当可观的性能.还有其他选择吗?
const tableName = "someTableName";
return r.expr(objs).forEach(function (docToInsert) {
return r.table(tableName).get(docToInsert('id')).replace(function (existingDoc) {
return r.branch(
existingDoc.eq(null),
r.expr(docToInsert).merge({ createdDT: r.now() }),
existingDoc.merge(docToInsert)
);
});
});
Run Code Online (Sandbox Code Playgroud)
编辑
标签和标题都不清楚:这个问题与rethinkDB有关
我需要在一个事务中更新 2 个表。
在当前版本中,RethinkDB 不支持开箱即用的事务。那么,我怎样才能做到这一点?
我可以通过两种方式进行更新:
但是,当 2 个更新中的 1 个完成得很好,但另一个没有时,我该如何解决?是的,我可以检查更新结果并在发生错误时恢复更新。但无论如何,可能会出现这样的情况,即应用程序发生某些事情(与 Rethink 的连接丢失,或者只是脚本崩溃),但两个更新中的一个已经完成。因此,我的数据库将处于不一致状态。并且没有办法解决这个问题。
那么,是否可以在 nodejs 中为 RethinkDB 模拟事务行为?
我在Node.js中将RethinkDB和hapi.js一起使用。我的主应用程序将打开的连接传递给hapi.js插件。在主脚本中使用连接可以正常工作,但是在插件中运行某些操作会引发以下错误:
Unhandled rejection ReqlDriverError: First argument to `run` must be an open connection.
Run Code Online (Sandbox Code Playgroud)
我试图弄清楚连接是否关闭,但没有其他监听器触发。
connection.addListener('connect', () => { console.log('!!connect') })
connection.addListener('close', () => { console.log('!!closed') })
connection.addListener('timeout', () => { console.log('!!timeout') })
connection.addListener('error', () => { console.log('!!error') })
Run Code Online (Sandbox Code Playgroud)
我可以确认事件列表器总体上可以正常工作,就像connection.close()手动调用输出一样!!closed。
我问了GitHub上的hapi.js社区是否将传递的对象作为副本传递给插件,但实际上并非如此。
这是引发错误的代码:
console.log(server.root.app.connection) // {…} === connection
console.log(server.root.app.connection.open) // true
r.table('records').group('siteLocation').count().run(server.root.app.connection, (err, cursor) => {
...
})
Run Code Online (Sandbox Code Playgroud)
把它们加起来:
First argument to 'run' must be an open connection.有什么方法可以找出为什么连接不再打开?
RethinkDB版本:2.1.5
hapi.js版本:11.1.2
谢谢!
假设我有一个"类别"表,每个类别在"数据"表中都有关联的数据,并且它在"关联"的其他表中有相关数据,我想删除一个包含所有相关数据的类别.我目前正在做的是这样的:
getAllDataIdsFromCategory()
.then(removeAllAssociated)
.then(handleChanges)
.then(removeDatas)
.then(handleChanges)
.then(removeCategory)
.then(handleChanges);
Run Code Online (Sandbox Code Playgroud)
有没有办法在db端链接这些查询?我的功能目前看起来像这样:
var getAllDataIdsFromCategory = () => {
return r
.table('data')
.getAll(categoryId, { index: 'categoryId' })
.pluck('id').map(r.row('id')).run();
}
var removeAllAssociated = (_dataIds: string[]) => {
dataIds = _dataIds;
return r
.table('associated')
.getAll(dataIds, { index: 'dataId' })
.delete()
.run()
}
var removeDatas = () => {
return r
.table('data')
.getAll(dataIds)
.delete()
.run()
}
Run Code Online (Sandbox Code Playgroud)
请注意,我不能使用r.expr()或r.do(),因为我想根据前一个查询的结果进行查询.我的方法的问题是它不适用于大量的"数据",因为我必须将所有的id带到客户端,并且在循环中在客户端进行分页似乎是一种解决方法.
我在包含用户ID列表的文档中有一个数组.
我想找到所有包含此用户ID列表的文档.
我知道我可以这样做:
r.db('unicorn')
.table('rooms').filter(function(doc){
return doc('people').contains(
"id-one","id-two"
)
})
Run Code Online (Sandbox Code Playgroud)
我知道这会很好用,但我必须硬编码.如何传递contains函数一组值来匹配?