我正在使用Macbook pro2016。在终端上,我打了“ mongod --port 27018”,一切正常。另外,Robo 3T显示该连接有效。问题是当运行“ python3 User.py”时出现错误:
mongoengine.connect(db="Telegram_User_DataBase", alias="telegramUDB", port=27018)
class User(mongoengine.Document):
name = mongoengine.StringField()
last_name = mongoengine.StringField()
username = mongoengine.StringField()
telegramID = mongoengine.StringField()
is_bot = mongoengine.BooleanField()
def newUser(self, name, last_name, username, telegramID, is_bot):
"""Cria um novo usuario e o salva no banco de dados Telegram_User_DataBase"""
self.name = name
self.last_name = last_name
self.username = username
self.telegramID = telegramID
self.is_bot = is_bot
self.save() #error goes right here
#testing
user1 = User()
user1.newUser("erick", "giff", "egf", "1234", False)
我期望程序将在端口27018上创建我的数据库,但是那没有发生。 我认为也许我在第一行做错了mongoengine.connect(...
答案 0 :(得分:0)
如果您有多个数据库,则需要使用meta
类属性将模型附加到数据库,否则默认情况下它们将附加到alias='default'
例如:
connect(db='test') # identified by alias `default`
connect(..., alias='telegramUDB')
class User(Document):
meta = {'db-alias': 'telegramUDB'}
name = StringField()
class Person(Document): # implicitly attached to alias=default
name = StringField()
如果只有1个数据库,最好在调用connect()时不要设置alias
,这样就不必处理bd-alias
查看此页面以获取更多信息:http://docs.mongoengine.org/guide/connecting.html#multiple-databases