Specifying collection name with MongoEngine

后端 未结 1 577
故里飘歌
故里飘歌 2021-01-12 07:50

Once content is added the name of the collection is defaulted to the name of the class. Is it possible to specify the collection name or is my approach wrong? Using the code

相关标签:
1条回答
  • 2021-01-12 08:14

    Didn't look at the docs properly. Here it is:

    2.3.4. Document collections

    Document classes that inherit directly from Document will have their own collection in the database. The name of the collection is by default the name of the class, converted to lowercase (so in the example above, the collection would be called page). If you need to change the name of the collection (e.g. to use MongoEngine with an existing database), then create a class dictionary attribute called meta on your document, and set collection to the name of the collection that you want your document class to use:

    class Page(Document):
        title = StringField(max_length=200, required=True)
        meta = {'collection': 'cmsPage'}
    
    0 讨论(0)
提交回复
热议问题