Update document after printing result

53 views Asked by At

I'm trying to retrieve a list of documents, do something with the returned documents, then update the status to flag its been processed. This is what I have:

cursor = r.db("stuff").table("test").filter(r.row["subject"] == "books").run()
for document in cursor:
    print(document["subject"])
    document.update({"processed": True})

This seems to run OK but the "processed" field does not get updated as I would have expected. I'm probably approaching this incorrectly, so any pointers would be appreciated here.

UPDATE

This seems to work OK but I can't help thinking its somewhat inefficient:

cursor = r.db("stuff").table("test").filter(r.row["subject"] == "books").run()
for document in cursor:
    print(document["subject"])
    r.db("certs").table("test").get(document['id']).update({"tpp_processed": True}).run()
1

There are 1 answers

0
Jorge Silva On

1. Using a for_each

Instead of doing an update with a run every time you want to update a single document, you can saves the changes in an array and then use forEach to update all the documents in one query. This would look something like this:

cursor = r.table('30693613').filter(r.row["subject"] == "book").run(conn)
arr = list(cursor)
for row in arr:
    row['processed'] = True
r.expr(arr)
  .for_each(lambda row: r.table('30693613').get(row["id"]).update(row))
  .run(conn)

Instead of doing N networks calls for every update, this will only execute one network call.

2. Building an update array and using forEach

You can also do something similar in which you build an array and you just run one query at the end:

cursor = r.db("stuff").table("test").filter(r.row["subject"] == "books").run() updated_rows = {} for document in cursor: print(document["subject"]) updated_rows.append({ id: document["id"], "tpp_processed": True }

// Afterwards... r.expr(updated_rows) .for_each(lambda row: r.table('30693613').get(row["id"]).update(row)) .run(conn)

3. Using no_reply

Finally, you can keep your query exactly the same as it is and then just run with noreply. That wait, your code will just keep running and won't wait till the database gets back a response.

cursor = r.db("stuff").table("test").filter(r.row["subject"] == "books").run()
for document in cursor:
    print(document["subject"])
    r.db("certs").table("test").get(document['id']).update({"tpp_processed": True}).run(conn, noreply=True)