Mongoengine: 'background', 'unique' and 'sparse' are not valid for an _id index specification

Created on 24 Jan 2018  ·  15Comments  ·  Source: MongoEngine/mongoengine

=MongoDB-3.4 doesn't allow 'background', 'unique' or 'sparse' options when trying to create an index over an _id.

That will cause that mongodb returns an InvalidIndexSpecificationOption when MongoEngine try to create indexes for Document where no aditional index are provided by conf.

Here I attach a possible patch to fix this issue.
mongoengine_document_indexes.patch.txt

Most helpful comment

With mongodb >= 3.4, you shouldn't specify the unique=True on the primary_key anymore. The message is a bit confusing (its due to the fact that sparse=False gets specified by default on indexes) but I believe your issue is the unique=True. Please post your schema in case this does not work for you

All 15 comments

:+1: I have the same problem.

I had the same issue few weeks ago, since my distro update to mongodb 3.4 and since then I can't start one of the projects I have with mongoengine. Please, fix it as soon as you can because I can't go back to prior versions of mongodb. Thank you.

+1 MongoDB server version: 3.4.10

+1 MongoDB server version: 3.6.5

I am on it.

Hi, (and sorry for my english).
If you have a indexed field as a primary key in your document-model, you should not declare a indexed in meta because this make a error on call some methods like 'get', etc. So: if primary_key then, don't make this field as indexed.

Hi, Is there a reference to mongo official docs where it is stated that "MongoDB-3.4 doesn't allow 'background', 'unique' or 'sparse' options when trying to create an index over an _id." Thanks.

+1

+1 MongoDB server version: 3.6.5

Hi, I'm currently working on fixing the tests to support Mongo 3.4 in mongoengine (and travis) and I came across this issue.

The tests that had the issue were the ones that specify unique=True on the id field (being the primary key) like in:

    class Blog(Document):
        id = StringField(primary_key=True, unique=True)

    Blog(id='garbage').save() # raises OperationalFailure The field 'unique' is not valid for an _id index specification.

Removing the unique=True from there fixes the issue.

Could you provide the model for which you had the issue? I'd like to check if your problem is different than the one highlighted by the tests suite

when I set someone (unique=True, primary_key=True), it will raise "pymongo.errors.OperationFailure: The field 'sparse' is not valid for an _id index specification".

With mongodb >= 3.4, you shouldn't specify the unique=True on the primary_key anymore. The message is a bit confusing (its due to the fact that sparse=False gets specified by default on indexes) but I believe your issue is the unique=True. Please post your schema in case this does not work for you

Thank you very much. According to this guide, I solved this problem.


发件人: Bastien Gérard notifications@github.com
发送时间: 2019年3月11日 19:20
收件人: MongoEngine/mongoengine
抄送: songkang1993; Comment
主题: Re: [MongoEngine/mongoengine] 'background', 'unique' and 'sparse' are not valid for an _id index specification (#1727)

With mongodb >= 3.4, you shouldn't specify the unique=True on the primary_key anymore. The message is a bit confusing (its due to the fact that sparse=False gets specified by default on indexes) but I believe your issue is the unique=True. Please post your schema in case this does not work for you


You are receiving this because you commented.
Reply to this email directly, view it on GitHubhttps://github.com/MongoEngine/mongoengine/issues/1727#issuecomment-471683515, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AkluID9H0aOVnA6Je81ktxAkAsl2gv58ks5vVqxmgaJpZM4RrlpU.

@bagerard solution worked for me

Glad to hear that it helped, closing this issue now

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lamiskin picture lamiskin  ·  4Comments

ajdas-code picture ajdas-code  ·  4Comments

MateuszBelczowski picture MateuszBelczowski  ·  4Comments

nilay-gpt picture nilay-gpt  ·  4Comments

ZEROF picture ZEROF  ·  4Comments