【发布时间】:2019-10-22 07:01:05
【问题描述】:
我对 python 和 mongodb 还很陌生,我已经遇到了一个问题。
我正在尝试使用 mongodb 作为数据源将 nodejs 后端restapi“翻译”到烧瓶中。
使用the flask documentation,我能够配置我的应用程序以连接到我的本地 mongod。
我可以像这样从users 集合中获取值
def getUser():
usr = Users.objects(email="mail@mail.com")
return {
"user": usr,
}
当我通过 Postman 调用 API 时返回以下 JSON
{
"user": [
{
"__v": 0,
"_id": {
"$oid": "5da86dc651eac87d2a82e2e2"
},
"createdAt": {
"$date": 1571319238918
},
"email": "mail@mail.com",
"password": "$2b$10$hoH57R5GL1MrwqpuW4yEJ.wwLlyNgyfxQm2Mxb19wioYTPPsU9z7y",
"profil": {
"_id": {
"$oid": "5da86dc651eac87d2a82e2e3"
},
"checked": false,
"clapList": [],
"followerList": [],
"followingList": [],
"playpoint": 0
},
"updatedAt": {
"$date": 1571319477959
}
}
]
}
如您所见,我有一个包含一个用户的数组。当我尝试只获取一个对象时,如下所示:
def getUser():
usr = Users.objects(email="mail@mail.com").first()
return {
"user": usr,
}
我在 Postman 中返回 500 状态,在调试控制台中出现以下错误:mongoengine.errors.FieldDoesNotExist: The fields "{'__v'}" do not exist on the document "Users"
这是我的用户模型
import mongoengine as me
class Users(me.Document):
phone = me.StringField()
email = me.StringField()
password = me.StringField()
accountType = me.StringField()
createdAt = me.DateTimeField()
updatedAt = me.DateTimeField()
profil = me.EmbeddedDocumentField(Profil)
我已经尝试将__v 添加为InfField(),但我仍然遇到同样的错误。
__v 到底是什么,我应该重新尝试从头开始创建一个新数据库吗?
附加信息:
- mongodb 数据库和集合是使用 nodejs API 生成的
- 数据库中的用户是使用 nodejs API 生成的
【问题讨论】: