使用 MongoEngine 指定 collection 名称
Specifying collection name with MongoEngine
添加内容后,collection 的名称默认为 class 的名称。是否可以指定 collection 名称或者我的方法有误?使用代码,我的 collection 默认命名为 "mongo_engine_python"。
from mongoengine import *
try:
connect(
db='MongoEngine_Test',
host="mongodb://localhost:27017/"
)
print("Connection successful")
except:
print("Unable to connnect")
class MongoEnginePython(Document):
item_name = StringField(max_length=200, required=True)
item_price = IntField(default=0)
没有正确查看文档。这是:
2.3.4。文献集
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'}
添加内容后,collection 的名称默认为 class 的名称。是否可以指定 collection 名称或者我的方法有误?使用代码,我的 collection 默认命名为 "mongo_engine_python"。
from mongoengine import *
try:
connect(
db='MongoEngine_Test',
host="mongodb://localhost:27017/"
)
print("Connection successful")
except:
print("Unable to connnect")
class MongoEnginePython(Document):
item_name = StringField(max_length=200, required=True)
item_price = IntField(default=0)
没有正确查看文档。这是:
2.3.4。文献集
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'}