Repository navigation
Description
There is a whole class of document manipulation tasks that are extremely frustrating to perform in ReQL. Consider the following example document:
{
# primary key, a db_name/table_name pair; named "table"
"table": "db_name.table_name_1",
# list of shards for the table
"shards": [
{
# shard name
name: "shard1",
# list of replicas; must be the same length across all shards;
# servers are specified by name
"replicas": ["m1", "m2"],
# priority list of directors; must be a non-empty subset of replicas;
# this field may be omitted, in which case the system just uses the
# replica list above
"director_candidates": ["m2"]
},
{
# shard name
name: "shard2",
# list of replicas; must be the same length across all shards;
# servers are specified by name
"replicas": ["m1", "m2"],
# priority list of directors; must be a non-empty subset of replicas;
# this field may be omitted, in which case the system just uses the
# replica list above
"director_candidates": ["m2"]
}
]
}Suppose I'd like to change the second replica in shard2 from m2 to m3. Doing that in ReQL right now is really, really, really hard.
I propose introducing a concept of subobject selections, that would allow performing this operation as follows:
r.table('foo')
.get(doc_id)('shards')
.filter({ 'name': 'shard2'})('replicas').nth(1)
.update('m3')Commands like get_field, pluck, nth, and filter (called on selections of objects or arrays) would return subobject selections that could then be modified via update, delete, and replace directly. This is absolutely beautiful because I could incrementally build up a query for precisely the element I want, and then change it by tacking on a mutable command.
Some open questions I can think of:
- How do we make this work with our type system? What are the edge cases I didn't think through?
- This introduces a weird duality in commands -- if the object above isn't a document in the table and is passed via
r.expr, the code to return a new object with the second replica ofshard2modified is dramatically different (and much more complicated) from the code if the object is a document in the table. Is there anything we can do to unify these two use cases so the code looks similar?
Related to (and possibly supersedes) #2056.
Activity
AtnNn commentedon Jul 11, 2014
I like this idea.
However it seems to break down if you want to modify multiple parts of the document.
The "modifiable" state could be preserved when using
doandfor_each. And when modifying an object, the body of the function passed todoandfor_eachcould be an array of modifications to apply sequentially.Something like this:
There could be a command that turns an object into one that can be modified:
coffeemug commentedon Jul 11, 2014
For the second problem above, perhaps we can introduce a command to fix this. Something like the following (but with a better name):
coffeemug commentedon Jul 11, 2014
Err, @AtnNn beat me to it.
coffeemug commentedon Jul 11, 2014
Man, this would be super-duper-uber delightful.
coffeemug commentedon Jul 11, 2014
Moving to polish because this is important.
deontologician commentedon Jul 11, 2014
Expanding on @AtnNn 's example, multiple operations might be common enough for a syntax like:
srh commentedon Jul 11, 2014
You're making this too complicated. Updating multiple fields at once with weird particular updates can be done by doing a replace on a pluck, and if you want to update particular array entries in particular ways, a replace on the whole array, or just the whole document.
coffeemug commentedon Jul 11, 2014
It might be worth not worrying about multiple updates for the first implementation and making those changes the way @srh suggests. If we find that to be too painful, we can always reconsider.
AtnNn commentedon Jul 11, 2014
@srh The motivation behind the proposal is to avoid such updates because they are very hard to do in ReQL currently. Compare my protoype above with the following query that works in 1.13:
There is nothing elegant about this query. I'm used to helping users write such queries, but it is unreasonable to expect users to write and maintain queries like this.
My suggestion of using arrays however is just a shortcut. Perhaps it is sufficient to just chain
modify, as insequence.replace(r.row.modify(...).modify(...))coffeemug commentedon Jul 11, 2014
But there is a middle ground between the two. Consider:
vs
The latter one is actually ok IMO.
3 remaining items
AtnNn commentedon Jul 11, 2014
@coffeemug Those updates are still too close. I was talking about modifying candidates for one shard and the replicas for another. It would be a very ugly query.
Here are questions asked on the mailing list related to updating nested documents:
https://groups.google.com/forum/#!searchin/rethinkdb/update/rethinkdb/sSB-rtXQPsk/SrYFZ87riKoJ
https://groups.google.com/forum/#!searchin/rethinkdb/update/rethinkdb/ZfqWdQpVfKU/983eH9QB1VgJ
https://groups.google.com/forum/#!searchin/rethinkdb/update/rethinkdb/lVgHH04O2gc/CbuM_FPlTqMJ
https://groups.google.com/forum/#!searchin/rethinkdb/update/rethinkdb/Qm5izPuIoaw/875pLAAKC4IJ
https://groups.google.com/forum/#!searchin/rethinkdb/update/rethinkdb/BKsVhuwXEac/CKx1nZJhl4IJ
https://groups.google.com/forum/#!searchin/rethinkdb/update/rethinkdb/iSidKdUKIUY/FdAjUwa9xq8J
https://groups.google.com/forum/#!searchin/rethinkdb/update/rethinkdb/oAp8JnDxc4M/zl5YFcNRzDoJ
coffeemug commentedon Jul 11, 2014
That sounds pretty compelling.
coffeemug commentedon Jul 21, 2014
Note: copying @robert-zaremba's commend from #2729 (comment) here:
Currently if we want to manipulate the object and set some of it's fields we need to deconstruct it using
maporkeys.The use case is to allow writing more sophisticated function manipulation.
Eg something we could write in JS:
Proposal:
setmethod, which will manipulate an object:mlucy commentedon Jul 22, 2014
This seems like it's getting very close to #895.
ha1331 commentedon Aug 13, 2015
My idea would be:
functionThatDoesStuffIfConditionIsMetAndReturnsEveryDocumentExceptIfModeIsDelete(condition, action[,{mode: 'update'|'replace'|'delete'}), or: fTDSICIMAREDEIMID() .If this has some actual merit, you might want to figure out better name for this.
How would fTDSICIMAREDEIMID() work? Lets say you have a document that looks something like this:
Now you wanted to add a "like" to one of the comments, what you would do is:
in this example fTDSICIMAREDEIMID() would return:
If {mode: 'delete'} it would have returned the array without the one object that had id: "d" this delete mode is here mainly to enable well... deleting nested items.
"update" & "replace" modes work like update() and replace(), "delete" mode is for convenience. I think this proposition enables new ways to do things without going too far from how people have used ReQL before. I'm not an expert, but this seems to allow multiple updates to different fields inside a single update().
edit:
mode: "drop" I think is better than "delete", because it describes the action better. After all nothing gets deleted.
mike-marcacci commentedon Mar 11, 2016
I'd just like to point out that these semantics might be able to be reused for atomic, multi-document mutations if such a feature were implemented like this.
netojose commentedon Nov 28, 2019
5 years later, and this issue still opened? :o