Question

I'm designing an application using mongodb and python and I need some way to read a row then based on its contents update it but I don't know of a way to make sure than another process isn't accessing that row also. My searched for 'locking a row in mongodb' don't return anything useful.

I'm currently accessing the data using:

row = db.table.find_one({'_id' : id})
if row['value'] > 10:
    db.table.save(update_row(row))

But I worry that between row being created and the updated record being saved the data could have been accessed.

What sort of design pattern should i be using

Was it helpful?

Solution

Here is JavaScript Code to update the document only if it is original as initially read. Sorry I'm not good with python but you could use this code as a starting point.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top