1
votes

I have defined MongoEngine classes which are mapped with MongoDB. When I am trying to access the data using MongoEngine, at the specific code it is failing at first attempt but successfully returns data in the second attempt with the same code. Executing the code in python terminal

from Project.Mongo import User
user = User.objects(username = 'xyz@xyz.com').first()

from Project.Mongo import Asset
Asset.objects(org = user.org)

Last line from code generating the following error in first attempt.

Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.5/dist-packages/mongoengine/queryset/manager.py", line 37, in get queryset = queryset_class(owner, owner._get_collection()) File "/usr/local/lib/python3.5/dist-packages/mongoengine/document.py", line 209, in _get_collection cls.ensure_indexes() File "/usr/local/lib/python3.5/dist-packages/mongoengine/document.py", line 765, in ensure_indexes collection.create_index(fields, background=background, **opts) File "/usr/local/lib/python3.5/dist-packages/pymongo/collection.py", line 1754, in create_index self.__create_index(keys, kwargs, session, **cmd_options) File "/usr/local/lib/python3.5/dist-packages/pymongo/collection.py", line 1656, in __create_index session=session) File "/usr/local/lib/python3.5/dist-packages/pymongo/collection.py", line 245, in _command retryable_write=retryable_write) File "/usr/local/lib/python3.5/dist-packages/pymongo/pool.py", line 517, in command collation=collation) File "/usr/local/lib/python3.5/dist-packages/pymongo/network.py", line 125, in command parse_write_concern_error=parse_write_concern_error) File "/usr/local/lib/python3.5/dist-packages/pymongo/helpers.py", line 145, in _check_command_response raise OperationFailure(msg % errmsg, code, response) pymongo.errors.OperationFailure: Index: { v: 2, key: { org: 1, _fts: "text", _ftsx: 1 }, name: "org_1_name_content_text_description_text_content_text_tag_content_text_remote.source_text", ns: "digitile.asset", weights: { content: 3, description: 1, name_content: 10, remote.owner__name: 20, remote.source: 2, tag_content: 2 }, default_language: "english", background: false, language_override: "language", textIndexVersion: 3 } already exists with different options: { v: 2, key: { org: 1, _fts: "text", _ftsx: 1 }, name: "org_1_name_text_description_text_content_text_tag_content_text_remote.source_text", ns: "digitile.asset", default_language: "english", background: false, weights: { content: 3, description: 1, name: 10, remote.owner__name: 20, remote.source: 2, tag_content: 2 }, language_override: "language", textIndexVersion: 3 }

When I try same last line second time, it produces accurate result

I am using python 3.5.2 pymongo 3.7.2 mongoengine 0.10.6

1

1 Answers

1
votes

The first time you call .objects on a document class, mongoengine tries to create the indexes if they don't exist.

In this case it fails during the creation of an index on the asset collection (detail of indexes are taken from your Asset/User Document classes) as you can see in the error message: pymongo.errors.OperationFailure: Index: {...new index details...} already exists with different options {...existing index details...}.

The second time you make that call, mongoengine assumes that the indexes were created and isn't attempting to create it again, which explains why the second call passes.