gpt4 book ai didi

python - 使用 MongoEngine 指定集合名称

转载 作者:可可西里 更新时间:2023-11-01 09:11:07 37 4
gpt4 key购买 nike

添加内容后,集合的名称默认为类的名称。是否可以指定集合名称或者我的方法有误?使用代码,我的收藏默认命名为“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'}

关于python - 使用 MongoEngine 指定集合名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53976963/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com