Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB not allowing using '.' in key [duplicate]

Tags:

python

mongodb

People also ask

How do I fix duplicate key errors?

There are the following options for handling duplicate key errors: You can delete the file, for example if you know that there are values in the file that are not correct. You can then adjust the file and upload it again. The system will not transfer any data from files that contain duplicate instances.

What is duplicate key error?

A row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error. If you use the IGNORE keyword, errors that occur while executing the INSERT statement are treated as warnings instead.


You can set check_keys to False according to the source:

 test.insert(d,check_keys=False)


 def insert(self, doc_or_docs, manipulate=True,
           safe=None, check_keys=True, continue_on_error=False, **kwargs):

It does indeed work:

In [28]: d = {'.aaa' : '.bbb'}

In [29]: test.insert(d,check_keys=False)
Out[29]: ObjectId('54ea604bf9664e211e8ed4e6')

The docstring states:

  • check_keys (optional): If True check if keys start with '$' or contain '.', raising :class:~pymongo.errors.InvalidName in either case.

You seem to be able to use any character apart from just the two $ or . so a leading underscore or any other character would be fine and probably a better option.

There is info in the faq about escaping :

In some cases, you may wish to build a BSON object with a user-provided key. In these situations, keys will need to substitute the reserved $ and . characters. Any character is sufficient, but consider using the Unicode full width equivalents: U+FF04 (i.e. “$”) and U+FF0E (i.e. “.”).

And the dot-notation faq explains why using . is not a good idea:

MongoDB uses the dot notation to access the elements of an array and to access the fields of an embedded document. To access an element of an array by the zero-based index position, concatenate the array name with the dot (.) and zero-based index position, and enclose in quotes:


You can't use the '.aaa' as your key because Mongo uses the dot to refer to nested documents.

If you want to have the key look like a dot, you can use a unicode equivalent like \u002E:

>>> d = {'\u002Eaaa' : '\u002Ebbb'}    

However, I'd suggest your approach of just choosing another character and accepting it as a "limitation" of the platform.