The best way to ensure that a value in a collection is unique is to use a unique index.
In the MongoDB shell it would be:
db.users.ensureIndex({email: 1, unique: true})
Using PyMongo it's something like:
users.ensure_index([('email', 1)], unique=True)
The 1
in these examples orders values in the index in ascending order. This should be sufficient for your use. PyMongo offers pymongo.ASCENDING
and pymongo.DESCENDING
as conveniences.
More details are available in the docs.