为什么 MongoEngine/pymongo 仅在第一次尝试访问对象时出错
Why MongoEngine/pymongo giving error when trying to access object first time only
我定义了 MongoEngine 类,它映射到 MongoDB。当我尝试使用 MongoEngine 访问数据时,在特定代码处第一次尝试失败但第二次尝试使用相同代码成功 returns 数据。在 python 终端
中执行代码
from Project.Mongo import User
user = User.objects(username = 'xyz@xyz.com').first()
from Project.Mongo import Asset
Asset.objects(org = user.org)
第一次尝试时生成以下错误的代码的最后一行。
回溯(最近调用最后):
文件“”,第 1 行,位于
文件“/usr/local/lib/python3.5/dist-packages/mongoengine/queryset/manager.py”,第 37 行,在 get 中
queryset = queryset_class(owner, owner._get_collection())
文件“/usr/local/lib/python3.5/dist-packages/mongoengine/document.py”,第 209 行,在 _get_collection 中
cls.ensure_indexes()
文件“/usr/local/lib/python3.5/dist-packages/mongoengine/document.py”,第 765 行,在 ensure_indexes 中
collection.create_index(字段,背景=背景,**选择)
文件“/usr/local/lib/python3.5/dist-packages/pymongo/collection.py”,第 1754 行,在 create_index 中
self.__create_index(键、kwargs、会话、**cmd_options)
文件“/usr/local/lib/python3.5/dist-packages/pymongo/collection.py”,第 1656 行,在 __create_index
会话=会话)
文件“/usr/local/lib/python3.5/dist-packages/pymongo/collection.py”,第 245 行,在 _command 中
retryable_write=retryable_write)
文件“/usr/local/lib/python3.5/dist-packages/pymongo/pool.py”,第 517 行,在命令中
排序规则=排序规则)
文件“/usr/local/lib/python3.5/dist-packages/pymongo/network.py”,第 125 行,在命令中
parse_write_concern_error=parse_write_concern_error)
文件“/usr/local/lib/python3.5/dist-packages/pymongo/helpers.py”,第 145 行,在 _check_command_response 中
引发操作失败(消息 % errmsg,代码,响应)
pymongo.errors.OperationFailure: 索引: { v: 2, key: { org: 1, _fts: "text", _ftsx: 1 }, 名称: "org_1_name_content_text_description_text_content_text_tag_content_text_remote.source_text", ns: "digitile.asset", 权重: { 内容: 3, 描述: 1, name_content: 10, remote.owner__name: 20, remote.source: 2, tag_content: 2 }, default_language: "english", background: false, language_override: "language", textIndexVersion: 3 } 已经存在不同的选项:{ v: 2, key: { org: 1, _fts: "text" , _ftsx: 1 }, 名称: "org_1_name_text_description_text_content_text_tag_content_text_remote.source_text", ns: "digitile.asset", default_language: "english", 背景: false, 权重: { 内容: 3, 描述: 1, 名称: 10, remote.owner__name: 20, remote.source: 2, tag_content: 2 }, language_override: "language", textIndexVersion: 3 }
当我第二次尝试相同的最后一行时,它产生了准确的结果
我正在使用 python 3.5.2
pymongo 3.7.2
mongoengine 0.10.6
第一次对文档 class 调用 .objects
时,如果索引不存在,mongoengine 会尝试创建索引。
在这种情况下,它在 asset
集合上创建索引时失败(索引的详细信息取自您的 Asset/User
文档 classes),如您所见错误信息:
pymongo.errors.OperationFailure: Index: {...new index details...} already exists with different options {...existing index details...}
.
第二次调用时,mongoengine 假定索引已创建并且不会尝试再次创建它,这解释了第二次调用通过的原因。
我定义了 MongoEngine 类,它映射到 MongoDB。当我尝试使用 MongoEngine 访问数据时,在特定代码处第一次尝试失败但第二次尝试使用相同代码成功 returns 数据。在 python 终端
中执行代码from Project.Mongo import User
user = User.objects(username = 'xyz@xyz.com').first()
from Project.Mongo import Asset
Asset.objects(org = user.org)
第一次尝试时生成以下错误的代码的最后一行。
回溯(最近调用最后): 文件“”,第 1 行,位于 文件“/usr/local/lib/python3.5/dist-packages/mongoengine/queryset/manager.py”,第 37 行,在 get 中 queryset = queryset_class(owner, owner._get_collection()) 文件“/usr/local/lib/python3.5/dist-packages/mongoengine/document.py”,第 209 行,在 _get_collection 中 cls.ensure_indexes() 文件“/usr/local/lib/python3.5/dist-packages/mongoengine/document.py”,第 765 行,在 ensure_indexes 中 collection.create_index(字段,背景=背景,**选择) 文件“/usr/local/lib/python3.5/dist-packages/pymongo/collection.py”,第 1754 行,在 create_index 中 self.__create_index(键、kwargs、会话、**cmd_options) 文件“/usr/local/lib/python3.5/dist-packages/pymongo/collection.py”,第 1656 行,在 __create_index 会话=会话) 文件“/usr/local/lib/python3.5/dist-packages/pymongo/collection.py”,第 245 行,在 _command 中 retryable_write=retryable_write) 文件“/usr/local/lib/python3.5/dist-packages/pymongo/pool.py”,第 517 行,在命令中 排序规则=排序规则) 文件“/usr/local/lib/python3.5/dist-packages/pymongo/network.py”,第 125 行,在命令中 parse_write_concern_error=parse_write_concern_error) 文件“/usr/local/lib/python3.5/dist-packages/pymongo/helpers.py”,第 145 行,在 _check_command_response 中 引发操作失败(消息 % errmsg,代码,响应) pymongo.errors.OperationFailure: 索引: { v: 2, key: { org: 1, _fts: "text", _ftsx: 1 }, 名称: "org_1_name_content_text_description_text_content_text_tag_content_text_remote.source_text", ns: "digitile.asset", 权重: { 内容: 3, 描述: 1, name_content: 10, remote.owner__name: 20, remote.source: 2, tag_content: 2 }, default_language: "english", background: false, language_override: "language", textIndexVersion: 3 } 已经存在不同的选项:{ v: 2, key: { org: 1, _fts: "text" , _ftsx: 1 }, 名称: "org_1_name_text_description_text_content_text_tag_content_text_remote.source_text", ns: "digitile.asset", default_language: "english", 背景: false, 权重: { 内容: 3, 描述: 1, 名称: 10, remote.owner__name: 20, remote.source: 2, tag_content: 2 }, language_override: "language", textIndexVersion: 3 }
当我第二次尝试相同的最后一行时,它产生了准确的结果
我正在使用 python 3.5.2 pymongo 3.7.2 mongoengine 0.10.6
第一次对文档 class 调用 .objects
时,如果索引不存在,mongoengine 会尝试创建索引。
在这种情况下,它在 asset
集合上创建索引时失败(索引的详细信息取自您的 Asset/User
文档 classes),如您所见错误信息:
pymongo.errors.OperationFailure: Index: {...new index details...} already exists with different options {...existing index details...}
.
第二次调用时,mongoengine 假定索引已创建并且不会尝试再次创建它,这解释了第二次调用通过的原因。