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. Using a
for_each
Instead of doing an
update
with arun
every time you want to update a single document, you can saves the changes in an array and then useforEach
to update all the documents in one query. This would look something like this: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
withnoreply
. That wait, your code will just keep running and won't wait till the database gets back a response.