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.
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'))
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.
If you use the without
command, you won't have to do anything to take advantage of these optimizations (other than upgrading the server).
Hope this helps.
来源:https://stackoverflow.com/questions/17832931/rethinkdb-removing-data-from-documents