Rethinkdb 从文档中删除数据 [英] Rethinkdb removing data from documents

查看:67
本文介绍了Rethinkdb 从文档中删除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从具有给定相当简单结构的文档中删除一些数据部分,随着项目的进行,这些数据将变得更深更重:

I'm trying to remove some portions of data from documents with given fairly simple structure, which will get much deeper and heavier than this as the project goes:

{
    id: "...",
    name: "...",
    phone: "...",
    data: {
        key1: "val1",
        ...
    }
    ...
}

我知道除了用更新的树替换整个树之外,没有办法从嵌套部分更新/删除部分.

I'm aware that there is no way of updating/removing sections from the nested parts other than replacing the whole tree with updated tree.

例如,如果我想从文档数据中删除 key1,我需要使用不包含 key1 的副本更新文档数据部分

For example, if I want to delete key1 from document data, I need to update the documents data section with a copy of it where key1 is not contained

document.update({data: new dict without key1})

是否有任何更简单的方法可以从文档的根目录中删除一部分(例如名称字段),而无需使用不包含名称键和值的自身副本更新整个文档?每次需要删除某些部分数据时,是否都必须对文档进行深度复制和过滤?

Is there any eaiser way of deleting a portion from the root of document -like name field- without updating the whole document with a copy of itself that does not contain the name key and value? Do I have to deep copy and filter the document every time i need to remove some portions of data?

推荐答案

下面是一个从文档根中删除一个键的查询:

Below is a query that removes a key from the root of the document:

r.table('foo').get(document_id).replace(r.row.without('key'))

您也可以对多个文档进行如下操作:

You can also do it for multiple documents as follows:

r.table('foo').filter(condition).replace(r.row.without('key'))

从即将发布的 1.8 版本开始,您还可以按如下方式对嵌套键执行此操作:

As of the upcoming 1.8 release, you will also be able to do it for nested keys as follows:

r.table('foo').get(document_id).replace(r.row.without({data: { key1: true}}))

目前,上面的命令基本上用服务器上没有相关密钥的自身副本替换了文档.在接下来的几个版本中,这将进行大量优化,以最大程度地减少内存中的文档复制(因此,虽然看起来您正在用没有密钥的自身副本替换文档,但在幕后操作将在没有任何复制的情况下破坏性地执行).未来的版本可能会更新底层结构,这样整个文档就不必写入磁盘了.

Currently, the commands above essentially replace the document with the copy of itself without the relevant keys on the server. In the next few releases this will be heavily optimized to minimize document copying in memory (so while it looks like you're replacing the document with a copy of itself without a key, under the hood the operation will be performed destructively without any copying). Future releases might update the underlying structure so that the full document won't have to be written to disk.

如果您使用 without 命令,则无需执行任何操作即可利用这些优化(除了升级服务器).

If you use the without command, you won't have to do anything to take advantage of these optimizations (other than upgrading the server).

希望这会有所帮助.

这篇关于Rethinkdb 从文档中删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆