How to create an index in mongoengine to be unique = True and sparse = True

I use mongoengine with a jar. I have a db.Document class called a profile in which I want the field to be zero and unique, I understand how to do this to make the index of this field, which is sparse = True and unique = True, how to do it go about it?

+7
source share
2 answers

You need to declare an index in the meta definition, for example:

 class BlogPost(Document): date = DateTimeField(db_field='addDate', default=datetime.now) category = StringField() tags = ListField(StringField()) meta = { 'indexes': [ {'fields': ['-date'], 'unique': True, 'sparse': True, 'types': False }, ], } 
+12
source

In the case of a unique restriction, you can set it with a field declaration as:

 email = mongodb.EmailField(required=True, unique=True) 
+3
source

All Articles