lolnoob lolnooa
lolnoob lolnooa

Reputation: 59

MongoDB error when inserting values with contain `.`

{'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,
            }
        )

Hey. I'm trying to insert this into my MongoDB database. However, i get the error above which says the question cannot contain .. How can I fix this without having to remove the punctuation from the question? As it would be ideal to have this.

Upvotes: 0

Views: 77

Answers (1)

hhharsha36
hhharsha36

Reputation: 3349

Short answer: You can't.

It was possible in MongoDB version <= 3.6 where there was an option to bypass the validation completely using the check_keys=False option. But this option is deprecated ever since.

Although MongoDB has bypass_document_validation=True in newer versions, it won't bypass the . character for obvious reasons.

There is an open issue regarding this on JIRA (SERVER-30575) and it is still open. Also, the PyMongo specific issue related to this was raised once PYTHON-1522.

So until the SERVER-30575 issue fixed, you have no other choice.

I would suggest you rearrange your schema design to embed the key name as a value to a different key.

Something like this:

question = [
    {
        "question": "question1.",
        "answer": "no.",
    },
    {
        "question": "question2.",
        "answer": "yes.",
    },
]

Upvotes: 1

Related Questions