You may consider using the FormEncode or Colander Python libraries to help with such validation.10gen refer to this type of write operation as an upsert.In PyMongo, there are three possible methods one can use to perform an upsert.If a document already exists, update it.Otherwise create a new document.Having a single method which is capable of both modes of operation reduces the need for conditional handling in your client code, thus simplifying your program.This method either edits an existing document, or inserts a new one.Furthermore, semantics of the method dictate that the method will always be called with a session_id, but that the session_id may or may not already be present in the database.# We must query first, becase we don’t know whether this session_id already exists.That may not accurately reflect what is happening internally in the daemon or driver, but that is equivalent to whatever does go on.For example, imagine a billing system.There is an obvious race condition.That is to say, users expect changes they make to be reflected within the application instantly.Perhaps you are also ranking by high score this week, or this month, or even this year.This pattern isn’t limited to high scores.Not only do these counts need to be fast to read from the database, they needs to be quick to write.Additionally, with potentially millions of users, the data set can grow very large, very quickly.You might be tempted to keep only a detailed log, with one document per change.Totals for the various time periods can then be calculated by an aggregate query across the collection.This may work well initially, with only hundreds or thousands of documents to be aggregated to compute the result.Of course, as with many problems in Computer Science, the solution is ultimately a form of caching.For the weekly resolution score counts, we can name the properties after the current week number.There is no aggregation to perform whatsoever.With this pattern, we can also write very quickly and safely.Atomic update operators are great because they ensure the underlying data is in a consistent state and help to avoid nasty race conditions.Especially when dealing with billing, accurate counts are very important.This pattern can help greatly with high speed counting.This summary data is most useful for extremely fast reads and writes.Pylons, Pyramid and Django.This kind of framework is excellent when you aren’t exactly sure what pieces you will need when you are starting work on your application.First you need to create a virtual environment for your project.These instructions assume you have the virtualenv tool installed on your system.Open the setup.py file present in it with your favourite editor.Change the install_requires list to include the string pymongo.First, we shall create a config file for development