Question

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)?

Was it helpful?

Solution

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top