Support optimistic locking.

Based on https://github.com/MongoEngine/mongoengine/pull/276.