python - 插入包含 `.` 的值时出现 MongoDB 错误
问题描述
{'question1.': 'no', 'question2.': 'no.'}
Future exception was never retrieved
future: <Future finished exception=InvalidDocument("key 'question1.' must not contain '.'")>
print(answers)
self.client.applicants.insert_one(
{
"name": args,
"userId": ctx.author.id,
"guildId": ctx.guild.id,
"questions": answers,
"messageId": application_message.id,
}
)
嘿。我正在尝试将其插入到我的 MongoDB 数据库中。但是,我收到上面的错误,上面说问题不能包含.
。我怎样才能解决这个问题而不必从问题中删除标点符号?因为拥有这个将是理想的。
解决方案
简短的回答:你不能。
在 MongoDB 版本 <= 3.6 中可以选择使用该选项完全绕过验证check_keys=False
。但从那以后,这个选项就被弃用了。
尽管 MongoDB 有较新的版本,但出于显而易见的原因bypass_document_validation=True
,它不会绕过该字符。.
在 JIRA (SERVER-30575)上有一个未解决的问题,它仍然是开放的。此外,PyMongo
与此相关的具体问题曾在PYTHON-1522中提出。
因此,在SERVER-30575问题修复之前,您别无选择。
我建议您重新安排架构设计,将键名作为值嵌入到不同的键中。
像这样的东西:
question = [
{
"question": "question1.",
"answer": "no.",
},
{
"question": "question2.",
"answer": "yes.",
},
]
推荐阅读
- laravel - Laravel 信封 MIX_VAR 显示错误值
- reactjs - FilePond 仅接受 pdf 文件
- python-3.x - 从雅虎财经的字符串中解析日期时间
- javascript - 如何解决茉莉花错误“加载时出错:脚本错误。” 什么时候使用jquery?
- javascript - 如何使用函数在反应中插入重复
- wordpress - 精选图片未出现
- reactjs - 每当 React Formik 发生错误时更改现有输入字段的样式
- r - 在 MacOS 上从 R 运行 psql : sh: psql: command not found
- javascript - Javascript:更新矩阵中的列
- node.js - 无服务器离线开始工作,但部署时出现内部服务器错误