Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RethinkDB: removing item from array in one table by value from another table

Tags:

rethinkdb

I use RethinkDB in my project and have the following table structure:

data_item {
    id: "generated_thing",
    slug: "slug"
}

aggregation_of_data_items {
    items: ["some", "ids", "from", "data_item", "table"]
}

When I delete item from content table I want to keep data consistent - delete ID from aggregation_of_data_items.items array - is there any opportunity to do this in one request (something like $pull or $pullAll in MongoDB)?

like image 650
La Faulx Avatar asked Dec 16 '13 14:12

La Faulx


1 Answers

To delete an item from an array you can do the following (this is in Python but it works in any supported language):

def remove(doc, value):
  doc.replace(
    lambda doc: doc.merge({"items" : doc["items"].set_difference([value])}))

Now we just need to run a query that does both, the easiest way to do this is to put them in an array:

[r.table("data_item").get(id).delete(),
 remove(r.table("aggregation_of_..").get(...), id)]
.run()
like image 134
Joe Doliner Avatar answered Oct 10 '22 10:10

Joe Doliner