Created
March 12, 2012 03:55
-
-
Save codeb2cc/2019648 to your computer and use it in GitHub Desktop.
MongoEngine Index Definition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
from pprint import pprint | |
import pymongo, mongoengine | |
class TheDocument(mongoengine.Document): | |
field_a = mongoengine.StringField() | |
field_b = mongoengine.StringField() | |
field_c = mongoengine.StringField() | |
meta = { | |
'indexes': [ | |
{ | |
'fields': 'field_b', | |
'unique': True, | |
}, | |
{ | |
'fields': ('field_a', 'field_c', ), | |
}, | |
], | |
'allow_inheritance': False, | |
} | |
if __name__ == '__main__': | |
connection = pymongo.Connection('127.0.0.1', 27017) | |
db = connection['TestDB'] | |
mongoengine.connect('TestDB', host='127.0.0.1', port=27017) | |
try: | |
doc_a = TheDocument( | |
field_a = 'Hello World!', | |
field_b = 'Hello XingCloud!', | |
field_c = 'Hello Codeb2cc!', | |
) | |
doc_a.save() | |
collection = db[TheDocument._get_collection_name()] | |
pprint(collection.index_information()) | |
except Exception as e: | |
print e | |
connection.drop_database('TestDB') | |
connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment